MercuryDPM  Alpha
 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...
 
void read (std::istream &is)
 
void write (std::ostream &os) const
 
Mdouble getRandomNumber (Mdouble min, Mdouble max)
 This is a random generating routine can be used for initial positions. More...
 
Mdouble operator() (Mdouble min, Mdouble max)
 
Mdouble operator() ()
 
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:140
void seedLaggedFibonacciGenerator()
This seed the LFG.
Definition: RNG.cc:135
unsigned long int q_
Definition: RNG.h:145
unsigned long int randomSeedLinearCongruentialGenerator_
This is the initial seed of the RNG.
Definition: RNG.h:130
unsigned long int p_
This are the parameters that control the LFG random generator.
Definition: RNG.h:145
std::vector< Mdouble > randomSeedLaggedFibonacciGenerator_
This is the seeds required for the LFG.
Definition: RNG.h:135
unsigned long int a_
This are the two parameters that control the LCG random generated.
Definition: RNG.h:140
RNGType type_
This is the type of random number generator.
Definition: RNG.h:150
unsigned long int m_
Definition: RNG.h:140

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 101 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(), operator()(), ChuteBottom::setupInitialConditions(), and test().

102 {
103  switch (type_)
104  {
109  }
110 }
Mdouble getRandomNumberFromLaggedFibonacciGenerator(Mdouble min, Mdouble max)
This is a Lagged Fibonacci Generator.
Definition: RNG.cc:147
Mdouble getRandomNumberFromLinearCongruentialGenerator(Mdouble min, Mdouble max)
This is a basic Linear Congruential Generator Random.
Definition: RNG.cc:115
RNGType type_
This is the type of random number generator.
Definition: RNG.h:150
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 147 of file RNG.cc.

References p_, q_, and randomSeedLaggedFibonacciGenerator_.

Referenced by getRandomNumber().

148 {
149  Mdouble new_seed = fmod(randomSeedLaggedFibonacciGenerator_[0] + randomSeedLaggedFibonacciGenerator_[p_ - q_], static_cast<Mdouble>(1.0));
150  //Update the random seed
151  for (unsigned int i = 0; i < p_ - 1; i++)
152  {
154  }
155  randomSeedLaggedFibonacciGenerator_[p_ - 1] = new_seed;
156 
157  //Generate a random number in the required range
158 
159  Mdouble random_num;
160 
161  Mdouble range = max - min;
162  random_num = min + range * new_seed;
163 
164  return random_num;
165 }
unsigned long int q_
Definition: RNG.h:145
double Mdouble
unsigned long int p_
This are the parameters that control the LFG random generator.
Definition: RNG.h:145
std::vector< Mdouble > randomSeedLaggedFibonacciGenerator_
This is the seeds required for the LFG.
Definition: RNG.h:135
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 115 of file RNG.cc.

References a_, c_, m_, and randomSeedLinearCongruentialGenerator_.

Referenced by getRandomNumber(), and seedLaggedFibonacciGenerator().

116 {
117  //Update the random seed
119 
120  //Generate a random number in the required range
121 
122  Mdouble random_num;
123 
124  Mdouble range = max - min;
125  random_num = min + range * randomSeedLinearCongruentialGenerator_ / (static_cast<Mdouble>(m_) + 1.0);
126 
127  return random_num;
128 }
unsigned long int c_
Definition: RNG.h:140
double Mdouble
unsigned long int randomSeedLinearCongruentialGenerator_
This is the initial seed of the RNG.
Definition: RNG.h:130
unsigned long int a_
This are the two parameters that control the LCG random generated.
Definition: RNG.h:140
unsigned long int m_
Definition: RNG.h:140
Mdouble RNG::operator() ( Mdouble  min,
Mdouble  max 
)
inline

Definition at line 75 of file RNG.h.

References getRandomNumber().

76  {
77  return getRandomNumber(min, max);
78  }
Mdouble getRandomNumber(Mdouble min, Mdouble max)
This is a random generating routine can be used for initial positions.
Definition: RNG.cc:101
Mdouble RNG::operator() ( )
inline

Definition at line 80 of file RNG.h.

References getRandomNumber().

81  {
82  return getRandomNumber(0.0, 1.0);
83  }
Mdouble getRandomNumber(Mdouble min, Mdouble max)
This is a random generating routine can be used for initial positions.
Definition: RNG.cc:101
void RNG::randomise ( )

sets the random variables such that they differ for each run

Definition at line 91 of file RNG.cc.

References setRandomSeed().

Referenced by DPMBase::readNextArgument().

92 {
93  setRandomSeed(static_cast<unsigned long int>(time(nullptr)));
94 }
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::read ( std::istream &  is)

Definition at line 52 of file RNG.cc.

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

Referenced by DPMBase::read().

53 {
54  std::string dummy;
55  unsigned int type;
56  is >> type;
57  type_ = static_cast<RNGType>(type);
58  is >> a_;
59  is >> c_;
60  is >> m_;
61  is >> p_;
62  is >> q_;
64  //note: the seeds for the LaggedFibonacciGenerator cannot be restarted currently.
66  //randomSeedLaggedFibonacciGenerator_.resize(p_);
67  //for (auto& v : randomSeedLaggedFibonacciGenerator_)
68  // is >> v;
69 }
unsigned long int c_
Definition: RNG.h:140
void seedLaggedFibonacciGenerator()
This seed the LFG.
Definition: RNG.cc:135
unsigned long int q_
Definition: RNG.h:145
unsigned long int randomSeedLinearCongruentialGenerator_
This is the initial seed of the RNG.
Definition: RNG.h:130
unsigned long int p_
This are the parameters that control the LFG random generator.
Definition: RNG.h:145
unsigned long int a_
This are the two parameters that control the LCG random generated.
Definition: RNG.h:140
RNGType
Definition: RNG.h:38
RNGType type_
This is the type of random number generator.
Definition: RNG.h:150
unsigned long int m_
Definition: RNG.h:140
void RNG::seedLaggedFibonacciGenerator ( )
private

This seed the LFG.

Definition at line 135 of file RNG.cc.

References getRandomNumberFromLinearCongruentialGenerator(), p_, and randomSeedLaggedFibonacciGenerator_.

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

136 {
137  for (unsigned int i = 0; i < p_; i++)
138  {
140  }
141 }
Mdouble getRandomNumberFromLinearCongruentialGenerator(Mdouble min, Mdouble max)
This is a basic Linear Congruential Generator Random.
Definition: RNG.cc:115
unsigned long int p_
This are the parameters that control the LFG random generator.
Definition: RNG.h:145
std::vector< Mdouble > randomSeedLaggedFibonacciGenerator_
This is the seeds required for the LFG.
Definition: RNG.h:135
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 220 of file RNG.cc.

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

221 {
222  //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.
223  if (p > q)
224  {
225  p_ = p;
226  q_ = q;
227  }
228  else
229  {
230  p_ = p;
231  q_ = q;
232  }
233 
236 
237 }
void seedLaggedFibonacciGenerator()
This seed the LFG.
Definition: RNG.cc:135
unsigned long int q_
Definition: RNG.h:145
unsigned long int p_
This are the parameters that control the LFG random generator.
Definition: RNG.h:145
std::vector< Mdouble > randomSeedLaggedFibonacciGenerator_
This is the seeds required for the LFG.
Definition: RNG.h:135
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 84 of file RNG.cc.

References a_, c_, and m_.

85 {
86  a_ = a;
87  c_ = c;
88  m_ = m;
89 }
unsigned long int c_
Definition: RNG.h:140
unsigned long int a_
This are the two parameters that control the LCG random generated.
Definition: RNG.h:140
unsigned long int m_
Definition: RNG.h:140
void RNG::setRandomNumberGenerator ( RNGType  type)

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

Definition at line 96 of file RNG.cc.

References type_.

97 {
98  type=type_;
99 }
RNGType type_
This is the type of random number generator.
Definition: RNG.h:150
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:135
unsigned long int randomSeedLinearCongruentialGenerator_
This is the initial seed of the RNG.
Definition: RNG.h:130
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 172 of file RNG.cc.

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

173 {
174  //This are the fixed parameters that define the test
175  static unsigned int num_of_tests = 100000;
176  static Mdouble max_num = 100.0;
177  static unsigned int num_of_bins = 10;
178 
179  //This is the generated random_number
180  Mdouble rn;
181  //This is the bin the random number will lie in
182  unsigned int bin = 0;
183  //This is a vector of bins
184  std::vector<int> count;
185  count.resize(num_of_bins);
186 
187  //Initialisation of the bins
188  for (unsigned int i = 0; i < num_of_bins; i++)
189  {
190  count[bin] = 0;
191  }
192 
193  //Loop over a number of tests
194  for (unsigned int i = 0; i < num_of_tests; i++)
195  {
196  rn = getRandomNumber(0.0, max_num);
197  bin = static_cast<unsigned int>(std::floor(rn * num_of_bins / max_num));
198 
199  //Add one to the bin count
200  count[bin]++;
201 
202  }
203 
204  //Final post-process the result and report on the random number
205  Mdouble chi_cum = 0.0;
206  Mdouble expected = num_of_tests / num_of_bins;
207 
208  for (unsigned int i = 0; i < num_of_bins; i++)
209  {
210  chi_cum = chi_cum + (count[i] - expected) * (count[i] - expected) / expected;
211  std::cout << i << " : " << count[i] << " : " << (count[i] - expected) * (count[i] - expected) / expected << std::endl;
212  }
213  //end for loop over computing the chi-squared value.
214  std::cout << chi_cum << std::endl;
215 
216  return mathsFunc::chi_squared_prob(chi_cum, num_of_bins);
217 }
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...
double Mdouble
Mdouble getRandomNumber(Mdouble min, Mdouble max)
This is a random generating routine can be used for initial positions.
Definition: RNG.cc:101
void RNG::write ( std::ostream &  os) const

Definition at line 71 of file RNG.cc.

References a_, c_, m_, p_, q_, randomSeedLinearCongruentialGenerator_, and type_.

Referenced by DPMBase::write().

72 {
73  os << " " << static_cast<unsigned int>(type_);
74  os << " " << a_;
75  os << " " << c_;
76  os << " " << m_;
77  os << " " << p_;
78  os << " " << q_;
80  //for (auto v : randomSeedLaggedFibonacciGenerator_)
81  // os << " " << v;
82 }
unsigned long int c_
Definition: RNG.h:140
unsigned long int q_
Definition: RNG.h:145
unsigned long int randomSeedLinearCongruentialGenerator_
This is the initial seed of the RNG.
Definition: RNG.h:130
unsigned long int p_
This are the parameters that control the LFG random generator.
Definition: RNG.h:145
unsigned long int a_
This are the two parameters that control the LCG random generated.
Definition: RNG.h:140
RNGType type_
This is the type of random number generator.
Definition: RNG.h:150
unsigned long int m_
Definition: RNG.h:140

Member Data Documentation

unsigned long int RNG::a_
private

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

Definition at line 140 of file RNG.h.

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

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 145 of file RNG.h.

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

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

This is the seeds required for the LFG.

Definition at line 135 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 130 of file RNG.h.

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

RNGType RNG::type_
private

This is the type of random number generator.

Definition at line 150 of file RNG.h.

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


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