5 #include <HeliosException.h> 13 void setDefaultRandomnessGeneratorSeed(std::string
const seed =
"");
24 template <
typename RealType>
52 std::unique_ptr<std::mt19937>
urdGen =
nullptr;
56 std::unique_ptr<std::uniform_real_distribution<RealType>>
urd =
nullptr;
61 std::unique_ptr<std::mt19937>
ndGen =
nullptr;
65 std::unique_ptr<std::normal_distribution<RealType>>
nd =
nullptr;
98 mode(
"FIXED_SEED_DOUBLE"),
107 mode(
"FIXED_SEED_DOUBLE"),
108 doubleSeed((double)seed)
116 mode(
"FIXED_SEED_LONG"),
125 mode(
"FIXED_SEED_LONG"),
195 RealType lowerBound, RealType upperBound
231 extern bool DEFAULT_RG_MODIFIED_FLAG;
232 extern std::unique_ptr<RandomnessGenerator<double>> DEFAULT_RG;
242 template<
typename RealType>
244 bool isTimestamp =
false;
245 bool isDouble =
false;
246 for(
size_t i = 0 ; i < seedstr.length() ; i++){
247 if(seedstr[i] ==
':') isTimestamp =
true;
248 if(seedstr[i] ==
'.') isDouble =
true;
252 mode =
"FIXED_SEED_LONG";
254 longSeed += (std::stol(seedstr.substr(0,4))-1970)*31104000;
255 longSeed += std::stol(seedstr.substr(5,2))*2592000;
256 longSeed += std::stol(seedstr.substr(8,2))*86400;
257 longSeed += std::stol(seedstr.substr(11,2))*3600;
258 longSeed += std::stol(seedstr.substr(14,2))*60;
259 longSeed += std::stol(seedstr.substr(17,2));
262 mode =
"FIXED_SEED_DOUBLE";
266 mode =
"FIXED_SEED_LONG";
271 template<
typename RealType>
280 else this->
urdGen = std::unique_ptr<std::mt19937>(
281 new std::mt19937(*rg.
urdGen)
283 if(rg.
urd ==
nullptr) this->
urdGen =
nullptr;
285 std::unique_ptr<std::uniform_real_distribution<RealType>>(
286 new std::uniform_real_distribution<RealType>(*rg.
urd)
289 if(rg.
ndGen ==
nullptr) this->
ndGen =
nullptr;
290 else this->
ndGen = std::unique_ptr<std::mt19937>(
291 new std::mt19937(*rg.
ndGen)
293 if(rg.
nd ==
nullptr) this->
nd =
nullptr;
294 else this->
nd = std::unique_ptr<std::normal_distribution<RealType>>(
295 new std::normal_distribution<RealType>(*rg.
nd)
299 template<
typename RealType>
308 template<
typename RealType>
317 template<
typename RealType>
326 template<
typename RealType>
335 std::swap(rgA.
urd, rgB.
urd);
337 std::swap(rgA.
nd, rgB.
nd);
342 template <
typename RealType>
344 RealType lowerBound, RealType upperBound
346 if(
mode ==
"AUTO_SEED") {
347 std::random_device rd;
348 urdGen = std::unique_ptr<std::mt19937>(
new std::mt19937(rd()));
349 urd = std::unique_ptr<std::uniform_real_distribution<RealType>>(
350 new std::uniform_real_distribution<RealType>(
351 lowerBound, upperBound
354 else if(
mode ==
"FIXED_SEED_DOUBLE"){
355 urdGen = std::unique_ptr<std::mt19937>(
358 urd = std::unique_ptr<std::uniform_real_distribution<RealType>>(
359 new std::uniform_real_distribution<RealType>(
360 lowerBound, upperBound
363 else if(
mode ==
"FIXED_SEED_LONG"){
364 urdGen = std::unique_ptr<std::mt19937>(
367 urd = std::unique_ptr<std::uniform_real_distribution<RealType>>(
368 new std::uniform_real_distribution<RealType>(
369 lowerBound, upperBound
373 template <
typename RealType>
376 return (*
urd)(*urdGen);
379 template <
typename RealType>
381 RealType mean, RealType stdev
383 if(
mode ==
"AUTO_SEED") {
384 std::random_device rd;
385 ndGen = std::unique_ptr<std::mt19937>(
new std::mt19937(rd()));
386 nd = std::unique_ptr<std::normal_distribution<RealType>>(
387 new std::normal_distribution<RealType>(
391 else if(
mode ==
"FIXED_SEED_DOUBLE"){
392 ndGen = std::unique_ptr<std::mt19937>(
395 nd = std::unique_ptr<std::normal_distribution<RealType>>(
396 new std::normal_distribution<RealType>(
400 else if(
mode ==
"FIXED_SEED_LONG"){
401 ndGen = std::unique_ptr<std::mt19937>(
404 nd = std::unique_ptr<std::normal_distribution<RealType>>(
405 new std::normal_distribution<RealType>(
411 template <
typename RealType>
414 return (*
nd)(*ndGen);
Class to generate random numbers.
Definition: RandomnessGenerator.h:25
std::unique_ptr< std::mt19937 > ndGen
Normal Distribution Generator.
Definition: RandomnessGenerator.h:61
RandomnessGenerator()
Creates a RandomnessGenerator which will use an automatically computed seed.
Definition: RandomnessGenerator.h:91
RandomnessGenerator(long seed)
Creates a RandomnessGenerator which will use a long seed.
Definition: RandomnessGenerator.h:115
std::unique_ptr< std::uniform_real_distribution< RealType > > urd
Uniform Real Distribution.
Definition: RandomnessGenerator.h:56
double doubleSeed
Double seed for randomness.
Definition: RandomnessGenerator.h:43
std::unique_ptr< std::mt19937 > urdGen
Uniform Real Distribution Generator.
Definition: RandomnessGenerator.h:52
long longSeed
Long seed for randomness.
Definition: RandomnessGenerator.h:47
RandomnessGenerator(double seed)
Creates a RandomnessGenerator which will use a double seed.
Definition: RandomnessGenerator.h:97
RandomnessGenerator & operator=(RandomnessGenerator const &rg)
Copy assignment operator.
Definition: RandomnessGenerator.h:310
std::string mode
RandomnessGenerator mode.
Definition: RandomnessGenerator.h:38
std::unique_ptr< std::normal_distribution< RealType > > nd
Normal Distribution.
Definition: RandomnessGenerator.h:65
RandomnessGenerator(float seed)
Like RandomnessGenerator(double) constructor, the float is casted to a double.
Definition: RandomnessGenerator.h:106
double getDoubleSeed()
Obtain the seed in double format. This getter is expected to be used with mode FIXED_SEED_DOUBLE.
Definition: RandomnessGenerator.h:76
RandomnessGenerator(int seed)
Like RandomnessGenerator(long) constructor, the integer is casted to a long.
Definition: RandomnessGenerator.h:124
void swap(RandomnessGenerator &rgA, RandomnessGenerator &rgB)
Swap two RandomnessGenerator.
Definition: RandomnessGenerator.h:327
long getLongSeed()
Obtain the seed in long format. This getter is expected to be used with mode FIXED_SEED_LONG.
Definition: RandomnessGenerator.h:83
RealType uniformRealDistributionNext()
Obtain the next value in the computed uniform real distribution.
Definition: RandomnessGenerator.h:374
RealType normalDistributionNext()
Obtain the next value in the computed normal distribution.
Definition: RandomnessGenerator.h:412
void computeNormalDistribution(RealType mean, RealType stdev)
Compute a normal distribution using the specified real data type.
Definition: RandomnessGenerator.h:380
void computeUniformRealDistribution(RealType lowerBound, RealType upperBound)
Compute a uniform real distribution using the specified real data type.
Definition: RandomnessGenerator.h:343