This repository has been archived on 2020-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
Files
Indoor/math/distribution/Uniform.h
2018-10-25 11:50:12 +02:00

66 lines
1.3 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* © Copyright 2014 Urheberrechtshinweis
* Alle Rechte vorbehalten / All Rights Reserved
*
* Programmcode ist urheberrechtlich geschuetzt.
* Das Urheberrecht liegt, soweit nicht ausdruecklich anders gekennzeichnet, bei Frank Ebner.
* Keine Verwendung ohne explizite Genehmigung.
* (vgl. § 106 ff UrhG / § 97 UrhG)
*/
#ifndef UNIFORM_H
#define UNIFORM_H
#include <cmath>
#include <random>
#include "../random/RandomGenerator.h"
#include <type_traits>
namespace Distribution {
/** uniform distribution */
template <typename T> class Uniform {
private:
Random::RandomGenerator gen;
/** depending on T, Dist is either a uniform_real or uniform_int distribution */
typedef typename std::conditional< std::is_floating_point<T>::value, std::uniform_real_distribution<T>, std::uniform_int_distribution<T> >::type Dist;
Dist dist;
public:
/** ctor */
Uniform(const T min, const T max) : gen(RANDOM_SEED), dist(min, max) {
}
/** ctor with seed */
Uniform(const T min, const T max, const uint32_t seed) : gen(seed), dist(min, max) {
}
/** -1 to +1 */
static Uniform unit() {
return Uniform(-1, +1);
}
/** get a uniformaly distributed random number */
T draw() {
return dist(gen);
}
/** set the seed to use */
void setSeed(const long seed) {
gen.seed(seed);
}
};
}
#endif // UNIFORM_H