MercuryDPM  Beta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RNG Class Reference

This is a class that generates random numbers i.e. named the Random Number Generator (RNG). More...

#include <RNG.h>

Public Member Functions

 RNG ()
 default constructor More...
 
void setRandomSeed (unsigned long int new_seed)
 This is the seed for the random number generator. (note the call to seed_LFG is only required really if using that type of generator, but the other one is always required. More...
 
Mdouble getRandomNumber (Mdouble min, Mdouble max)
 This is a random generating routine can be used for initial positions. More...
 
Mdouble test ()
 This function tests the quality of random numbers, based on the chi-squared test. More...
 
void setLinearCongruentialGeneratorParmeters (unsigned const int a, unsigned const int c, unsigned const int m)
 This functions set the parameters for the LCG random number generator. It goes multiplier, addition, mod. More...
 
void randomise ()
 sets the random variables such that they differ for each run More...
 
void setLaggedFibonacciGeneratorParameters (const unsigned int p, const unsigned int q)
 This function sets the parameters for the LFG random number generator. More...
 
void setRandomNumberGenerator (RNGType type)
 Allows the user to set which random number generator is used. More...
 

Private Member Functions

Mdouble getRandomNumberFromLinearCongruentialGenerator (Mdouble min, Mdouble max)
 This is a basic Linear Congruential Generator Random. More...
 
Mdouble getRandomNumberFromLaggedFibonacciGenerator (Mdouble min, Mdouble max)
 This is a Lagged Fibonacci Generator. More...
 
void seedLaggedFibonacciGenerator ()
 This seed the LFG. More...
 

Private Attributes

unsigned long int randomSeedLinearCongruentialGenerator_
 This is the initial seed of the RNG. More...
 
std::vector< MdoublerandomSeedLaggedFibonacciGenerator_
 This is the seeds required for the LFG. More...
 
unsigned long int a_
 This are the two parameters that control the LCG random generated. More...
 
unsigned long int c_
 
unsigned long int m_
 
unsigned long int p_
 This are the parameters that control the LFG random generator. More...
 
unsigned long int q_
 
RNGType type_
 This is the type of random number generator. More...
 

Detailed Description

This is a class that generates random numbers i.e. named the Random Number Generator (RNG).

This is a stand-along class; but is encapsulated (used) by the MD class. To make it architecture safe the both LCG and function is hard codes i.e. does not use the internal C++ one.

Todo:
(AT) implement new C++-standard RNG instead of this one (Kudos on the hard work done here though ;). NB: maybe something for Mercury 2?

Definition at line 52 of file RNG.h.

Constructor & Destructor Documentation

RNG::RNG ( )

default constructor

This is a random number generator and returns a Mdouble within the range specified.

Todo:

{Thomas: This code does sth. when min>max; I would prefer to throw an error.}

{the random seed should be stored in restart}

Definition at line 33 of file RNG.cc.

References a_, c_, LAGGED_FIBONACCI_GENERATOR, m_, p_, q_, randomSeedLaggedFibonacciGenerator_, randomSeedLinearCongruentialGenerator_, seedLaggedFibonacciGenerator(), and type_.

34 {
36  a_ = 1103515245;
37  c_ = 12345;
38  m_ = 1024 * 1024 * 1024;
40  p_ = 607;
41  q_ = 273;
44 }
unsigned long int c_
Definition: RNG.h:126
void seedLaggedFibonacciGenerator()
This seed the LFG.
Definition: RNG.cc:103
unsigned long int q_
Definition: RNG.h:131
unsigned long int randomSeedLinearCongruentialGenerator_
This is the initial seed of the RNG.
Definition: RNG.h:116
unsigned long int p_
This are the parameters that control the LFG random generator.
Definition: RNG.h:131
std::vector< Mdouble > randomSeedLaggedFibonacciGenerator_
This is the seeds required for the LFG.
Definition: RNG.h:121
unsigned long int a_
This are the two parameters that control the LCG random generated.
Definition: RNG.h:126
RNGType type_
This is the type of random number generator.
Definition: RNG.h:136
unsigned long int m_
Definition: RNG.h:126

Member Function Documentation

Mdouble RNG::getRandomNumber ( Mdouble  min,
Mdouble  max 
)

This is a random generating routine can be used for initial positions.

Definition at line 69 of file RNG.cc.

References getRandomNumberFromLaggedFibonacciGenerator(), getRandomNumberFromLinearCongruentialGenerator(), LAGGED_FIBONACCI_GENERATOR, LINEAR_CONGRUENTIAL_GENERATOR, and type_.

Referenced by Chute::createBottom(), ChuteInsertionBoundary::generateParticle(), CubeInsertionBoundary::generateParticle(), HopperInsertionBoundary::generateParticle(), ChuteBottom::setupInitialConditions(), and test().

70 {
71  switch (type_)
72  {
77  }
78 }
Mdouble getRandomNumberFromLaggedFibonacciGenerator(Mdouble min, Mdouble max)
This is a Lagged Fibonacci Generator.
Definition: RNG.cc:115
Mdouble getRandomNumberFromLinearCongruentialGenerator(Mdouble min, Mdouble max)
This is a basic Linear Congruential Generator Random.
Definition: RNG.cc:83
RNGType type_
This is the type of random number generator.
Definition: RNG.h:136
Mdouble RNG::getRandomNumberFromLaggedFibonacciGenerator ( Mdouble  min,
Mdouble  max 
)
private

This is a Lagged Fibonacci Generator.

This is a basic Linear Fibonacci Generator Random Is described by three parameters, the multiplication a, the addition c and the mod m.

Definition at line 115 of file RNG.cc.

References p_, q_, and randomSeedLaggedFibonacciGenerator_.

Referenced by getRandomNumber().

116 {
117  Mdouble new_seed = fmod(randomSeedLaggedFibonacciGenerator_[0] + randomSeedLaggedFibonacciGenerator_[p_ - q_], static_cast<Mdouble>(1.0));
118  //Update the random seed
119  for (unsigned int i = 0; i < p_ - 1; i++)
120  {
122  }
123  randomSeedLaggedFibonacciGenerator_[p_ - 1] = new_seed;
124 
125  //Generate a random number in the required range
126 
127  Mdouble random_num;
128 
129  Mdouble range = max - min;
130  random_num = min + range * new_seed;
131 
132  return random_num;
133 }
unsigned long int q_
Definition: RNG.h:131
double Mdouble
unsigned long int p_
This are the parameters that control the LFG random generator.
Definition: RNG.h:131
std::vector< Mdouble > randomSeedLaggedFibonacciGenerator_
This is the seeds required for the LFG.
Definition: RNG.h:121
Mdouble RNG::getRandomNumberFromLinearCongruentialGenerator ( Mdouble  min,
Mdouble  max 
)
private

This is a basic Linear Congruential Generator Random.

This is a basic Linear Congruential Generator Random Is described by three parameters, the multiplication a, the addition c and the mod m.

Definition at line 83 of file RNG.cc.

References a_, c_, m_, and randomSeedLinearCongruentialGenerator_.

Referenced by getRandomNumber(), and seedLaggedFibonacciGenerator().

84 {
85  //Update the random seed
87 
88  //Generate a random number in the required range
89 
90  Mdouble random_num;
91 
92  Mdouble range = max - min;
93  random_num = min + range * randomSeedLinearCongruentialGenerator_ / (static_cast<Mdouble>(m_) + 1.0);
94 
95  return random_num;
96 }
unsigned long int c_
Definition: RNG.h:126
double Mdouble
unsigned long int randomSeedLinearCongruentialGenerator_
This is the initial seed of the RNG.
Definition: RNG.h:116
unsigned long int a_
This are the two parameters that control the LCG random generated.
Definition: RNG.h:126
unsigned long int m_
Definition: RNG.h:126
void RNG::randomise ( )

sets the random variables such that they differ for each run

Definition at line 59 of file RNG.cc.

References setRandomSeed().

Referenced by DPMBase::readNextArgument().

60 {
61  setRandomSeed(static_cast<unsigned long int>(time(nullptr)));
62 }
void setRandomSeed(unsigned long int new_seed)
This is the seed for the random number generator. (note the call to seed_LFG is only required really ...
Definition: RNG.cc:46
void RNG::seedLaggedFibonacciGenerator ( )
private

This seed the LFG.

Definition at line 103 of file RNG.cc.

References getRandomNumberFromLinearCongruentialGenerator(), p_, and randomSeedLaggedFibonacciGenerator_.

Referenced by RNG(), setLaggedFibonacciGeneratorParameters(), and setRandomSeed().

104 {
105  for (unsigned int i = 0; i < p_; i++)
106  {
108  }
109 }
Mdouble getRandomNumberFromLinearCongruentialGenerator(Mdouble min, Mdouble max)
This is a basic Linear Congruential Generator Random.
Definition: RNG.cc:83
unsigned long int p_
This are the parameters that control the LFG random generator.
Definition: RNG.h:131
std::vector< Mdouble > randomSeedLaggedFibonacciGenerator_
This is the seeds required for the LFG.
Definition: RNG.h:121
void RNG::setLaggedFibonacciGeneratorParameters ( const unsigned int  p,
const unsigned int  q 
)

This function sets the parameters for the LFG random number generator.

Definition at line 188 of file RNG.cc.

References p_, q_, randomSeedLaggedFibonacciGenerator_, and seedLaggedFibonacciGenerator().

189 {
190  //p must be greater than q so makes sure this is true. Not sure what happens if you set p=q, in the LFG alogrithm.
191  if (p > q)
192  {
193  p_ = p;
194  q_ = q;
195  }
196  else
197  {
198  p_ = p;
199  q_ = q;
200  }
201 
204 
205 }
void seedLaggedFibonacciGenerator()
This seed the LFG.
Definition: RNG.cc:103
unsigned long int q_
Definition: RNG.h:131
unsigned long int p_
This are the parameters that control the LFG random generator.
Definition: RNG.h:131
std::vector< Mdouble > randomSeedLaggedFibonacciGenerator_
This is the seeds required for the LFG.
Definition: RNG.h:121
void RNG::setLinearCongruentialGeneratorParmeters ( unsigned const int  a,
unsigned const int  c,
unsigned const int  m 
)

This functions set the parameters for the LCG random number generator. It goes multiplier, addition, mod.

Definition at line 52 of file RNG.cc.

References a_, c_, and m_.

53 {
54  a_ = a;
55  c_ = c;
56  m_ = m;
57 }
unsigned long int c_
Definition: RNG.h:126
unsigned long int a_
This are the two parameters that control the LCG random generated.
Definition: RNG.h:126
unsigned long int m_
Definition: RNG.h:126
void RNG::setRandomNumberGenerator ( RNGType  type)

Allows the user to set which random number generator is used.

Definition at line 64 of file RNG.cc.

References type_.

65 {
66  type=type_;
67 }
RNGType type_
This is the type of random number generator.
Definition: RNG.h:136
void RNG::setRandomSeed ( unsigned long int  new_seed)

This is the seed for the random number generator. (note the call to seed_LFG is only required really if using that type of generator, but the other one is always required.

Definition at line 46 of file RNG.cc.

References randomSeedLinearCongruentialGenerator_, and seedLaggedFibonacciGenerator().

Referenced by DPMBase::constructor(), and randomise().

47 {
50 }
void seedLaggedFibonacciGenerator()
This seed the LFG.
Definition: RNG.cc:103
unsigned long int randomSeedLinearCongruentialGenerator_
This is the initial seed of the RNG.
Definition: RNG.h:116
Mdouble RNG::test ( )

This function tests the quality of random numbers, based on the chi-squared test.

It reports a probability that the random number being generated are coming from a uniform distributed. If this number is less than 0.95, it is strongly advised that you change the parameters being used

Definition at line 140 of file RNG.cc.

References mathsFunc::chi_squared_prob(), and getRandomNumber().

141 {
142  //This are the fixed parameters that define the test
143  static unsigned int num_of_tests = 100000;
144  static Mdouble max_num = 100.0;
145  static unsigned int num_of_bins = 10;
146 
147  //This is the generated random_number
148  Mdouble rn;
149  //This is the bin the random number will lie in
150  unsigned int bin = 0;
151  //This is a vector of bins
152  std::vector<int> count;
153  count.resize(num_of_bins);
154 
155  //Initialisation of the bins
156  for (unsigned int i = 0; i < num_of_bins; i++)
157  {
158  count[bin] = 0;
159  }
160 
161  //Loop over a number of tests
162  for (unsigned int i = 0; i < num_of_tests; i++)
163  {
164  rn = getRandomNumber(0.0, max_num);
165  bin = static_cast<unsigned int>(std::floor(rn * num_of_bins / max_num));
166 
167  //Add one to the bin count
168  count[bin]++;
169 
170  }
171 
172  //Final post-process the result and report on the random number
173  Mdouble chi_cum = 0.0;
174  Mdouble expected = num_of_tests / num_of_bins;
175 
176  for (unsigned int i = 0; i < num_of_bins; i++)
177  {
178  chi_cum = chi_cum + (count[i] - expected) * (count[i] - expected) / expected;
179  std::cout << i << " : " << count[i] << " : " << (count[i] - expected) * (count[i] - expected) / expected << std::endl;
180  }
181  //end for loop over computing the chi-squared value.
182  std::cout << chi_cum << std::endl;
183 
184  return mathsFunc::chi_squared_prob(chi_cum, num_of_bins);
185 }
Mdouble chi_squared_prob(const Mdouble x, const unsigned int k)
This is the function which actually gives the probability back using a chi squared test...
Definition: ExtendedMath.cc:86
double Mdouble
Mdouble getRandomNumber(Mdouble min, Mdouble max)
This is a random generating routine can be used for initial positions.
Definition: RNG.cc:69

Member Data Documentation

unsigned long int RNG::a_
private

This are the two parameters that control the LCG random generated.

Definition at line 126 of file RNG.h.

Referenced by getRandomNumberFromLinearCongruentialGenerator(), RNG(), and setLinearCongruentialGeneratorParmeters().

unsigned long int RNG::c_
private
unsigned long int RNG::m_
private
unsigned long int RNG::p_
private

This are the parameters that control the LFG random generator.

Definition at line 131 of file RNG.h.

Referenced by getRandomNumberFromLaggedFibonacciGenerator(), RNG(), seedLaggedFibonacciGenerator(), and setLaggedFibonacciGeneratorParameters().

unsigned long int RNG::q_
private
std::vector<Mdouble> RNG::randomSeedLaggedFibonacciGenerator_
private

This is the seeds required for the LFG.

Definition at line 121 of file RNG.h.

Referenced by getRandomNumberFromLaggedFibonacciGenerator(), RNG(), seedLaggedFibonacciGenerator(), and setLaggedFibonacciGeneratorParameters().

unsigned long int RNG::randomSeedLinearCongruentialGenerator_
private

This is the initial seed of the RNG.

Definition at line 116 of file RNG.h.

Referenced by getRandomNumberFromLinearCongruentialGenerator(), RNG(), and setRandomSeed().

RNGType RNG::type_
private

This is the type of random number generator.

Definition at line 136 of file RNG.h.

Referenced by getRandomNumber(), RNG(), and setRandomNumberGenerator().


The documentation for this class was generated from the following files: