refactored random subsystem
added compile-time seed support
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
#define DRAWLIST_H
|
||||
|
||||
#include <vector>
|
||||
#include <random>
|
||||
|
||||
#include "Random.h"
|
||||
#include "../Assertions.h"
|
||||
|
||||
/**
|
||||
@@ -38,7 +38,7 @@ private:
|
||||
std::vector<Entry> elements;
|
||||
|
||||
/** random number generator */
|
||||
std::minstd_rand gen;
|
||||
RandomGenerator gen;
|
||||
|
||||
public:
|
||||
|
||||
|
||||
28
math/Random.h
Normal file
28
math/Random.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef RANDOM_H
|
||||
#define RANDOM_H
|
||||
|
||||
#include <cmath>
|
||||
#include <random>
|
||||
#include "../misc/Time.h"
|
||||
|
||||
#ifdef USE_FIXED_SEED
|
||||
#define RANDOM_SEED 1234
|
||||
#else
|
||||
#define RANDOM_SEED ( std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1) )
|
||||
#endif
|
||||
|
||||
//using RandomGenerator = std::minstd_rand;
|
||||
|
||||
class RandomGenerator : public std::minstd_rand {
|
||||
|
||||
public:
|
||||
|
||||
/** ctor with default seed */
|
||||
RandomGenerator() : std::minstd_rand(RANDOM_SEED) {;}
|
||||
|
||||
/** ctor with custom seed */
|
||||
RandomGenerator(result_type) : std::minstd_rand(RANDOM_SEED) {;}
|
||||
|
||||
};
|
||||
|
||||
#endif // RANDOM_H
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include <cmath>
|
||||
#include <random>
|
||||
|
||||
#include "../Random.h"
|
||||
|
||||
namespace Distribution {
|
||||
|
||||
@@ -14,13 +14,13 @@ namespace Distribution {
|
||||
|
||||
const T lambda;
|
||||
|
||||
std::minstd_rand gen;
|
||||
RandomGenerator gen;
|
||||
std::exponential_distribution<T> dist;
|
||||
|
||||
public:
|
||||
|
||||
/** ctor */
|
||||
Exponential(const T lambda) : lambda(lambda), dist(lambda) {
|
||||
Exponential(const T lambda) : lambda(lambda), gen(RANDOM_SEED), dist(lambda) {
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <cmath>
|
||||
#include <random>
|
||||
#include "../Random.h"
|
||||
|
||||
namespace Distribution {
|
||||
|
||||
@@ -15,14 +16,14 @@ namespace Distribution {
|
||||
const T sigma;
|
||||
const T _a;
|
||||
|
||||
std::minstd_rand gen;
|
||||
RandomGenerator gen;
|
||||
std::normal_distribution<T> dist;
|
||||
|
||||
public:
|
||||
|
||||
/** ctor */
|
||||
Normal(const T mu, const T sigma) :
|
||||
mu(mu), sigma(sigma), _a(1.0 / (sigma * std::sqrt(2.0 * M_PI))), gen(1234), dist(mu,sigma) {
|
||||
mu(mu), sigma(sigma), _a(1.0 / (sigma * std::sqrt(2.0 * M_PI))), gen(RANDOM_SEED), dist(mu,sigma) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
|
||||
#include <cmath>
|
||||
#include <random>
|
||||
#include "../Random.h"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
namespace Distribution {
|
||||
|
||||
@@ -11,14 +14,17 @@ namespace Distribution {
|
||||
|
||||
private:
|
||||
|
||||
std::minstd_rand gen;
|
||||
std::uniform_real_distribution<T> dist;
|
||||
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(1234), dist(min, max) {
|
||||
Uniform(const T min, const T max) : gen(RANDOM_SEED), dist(min, max) {
|
||||
|
||||
}
|
||||
/** get a uniformaly distributed random number */
|
||||
|
||||
Reference in New Issue
Block a user