MercuryDPM  0.10
 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. More...

#include <RNG.h>

Public Member Functions

 RNG ()
 
void set_RandomSeed (Mdouble new_seed)
 This is the seed for the random number generator. More...
 
Mdouble get_RN (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 set_LCGParms (int new_a, int new_c, int new_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 set_LFGParms (int new_p, int new_q)
 This function sets the parametes for the LFG random number generator. More...
 
void set_RNtypeLCG ()
 Make the random number generator based on LCG. More...
 
void set_RNtypeLFG ()
 Make the random number generator based on LFG. More...
 

Private Member Functions

Mdouble get_LCG (Mdouble min, Mdouble max)
 This is a basic Linear Congruential Generator Random. More...
 
Mdouble get_LFG (Mdouble min, Mdouble max)
 This is a Laggend Fibonacci Generator. More...
 
void seed_LFG ()
 This seed the LFG. More...
 

Private Attributes

unsigned long int random_seed_LCG
 This is the initiall seed of the RNG. More...
 
std::vector< Mdoublerandom_seed_LFG
 This is the seeds required for the LFG. More...
 
long int a
 This are the two parameters that control the LCG random generated. More...
 
long int c
 
long int m
 
long int p
 This are the parameters that control the LFG random generator. More...
 
long int q
 
int 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.

Definition at line 38 of file RNG.h.

Constructor & Destructor Documentation

RNG::RNG ( )
inline

Definition at line 42 of file RNG.h.

References a, c, m, p, q, random_seed_LCG, random_seed_LFG, seed_LFG(), and type.

42 {random_seed_LCG=0; a=1103515245; c=12345;m=1024*1024*1024;type=1;p=607;q=273;random_seed_LFG.resize(p); seed_LFG();}
long int a
This are the two parameters that control the LCG random generated.
Definition: RNG.h:88
long int c
Definition: RNG.h:88
long int m
Definition: RNG.h:88
void seed_LFG()
This seed the LFG.
Definition: RNG.cc:77
int type
This is the type of random number generator.
Definition: RNG.h:94
long int q
Definition: RNG.h:91
std::vector< Mdouble > random_seed_LFG
This is the seeds required for the LFG.
Definition: RNG.h:85
long int p
This are the parameters that control the LFG random generator.
Definition: RNG.h:91
unsigned long int random_seed_LCG
This is the initiall seed of the RNG.
Definition: RNG.h:82

Member Function Documentation

Mdouble RNG::get_LCG ( 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 55 of file RNG.cc.

References a, c, m, and random_seed_LCG.

Referenced by get_RN(), and seed_LFG().

56 {
57  //Update the random seed
59 
60 
61  //Generate a random number in the required range
62 
63  Mdouble random_num;
64 
65  Mdouble range=max-min;
66  random_num = min+range*random_seed_LCG/(static_cast<Mdouble>(m) + 1.0);
67 
68  return random_num;
69 }
long int a
This are the two parameters that control the LCG random generated.
Definition: RNG.h:88
long int c
Definition: RNG.h:88
long int m
Definition: RNG.h:88
double Mdouble
Definition: ExtendedMath.h:33
unsigned long int random_seed_LCG
This is the initiall seed of the RNG.
Definition: RNG.h:82
Mdouble RNG::get_LFG ( Mdouble  min,
Mdouble  max 
)
private

This is a Laggend 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 89 of file RNG.cc.

References p, q, and random_seed_LFG.

Referenced by get_RN().

90 {
91  Mdouble new_seed=fmod(random_seed_LFG[0]+random_seed_LFG[p-q],(Mdouble) 1.0);
92  //Update the random seed
93  for (int i=0;i<p-1;i++)
94  {
96  }
97  random_seed_LFG[p-1]=new_seed;
98 
99 
100 
101  //Generate a random number in the required range
102 
103  Mdouble random_num;
104 
105  Mdouble range=max-min;
106  random_num = min+range*new_seed;
107 
108  return random_num;
109 }
double Mdouble
Definition: ExtendedMath.h:33
long int q
Definition: RNG.h:91
std::vector< Mdouble > random_seed_LFG
This is the seeds required for the LFG.
Definition: RNG.h:85
long int p
This are the parameters that control the LFG random generator.
Definition: RNG.h:91
Mdouble RNG::get_RN ( Mdouble  min,
Mdouble  max 
)

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

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 32 of file RNG.cc.

References get_LCG(), get_LFG(), and type.

Referenced by InsertionBoundary::checkBoundaryActionsBeforeTimeStep(), Chute::create_bottom(), Chute::create_inflow_particle(), ChuteWithHopper::create_inflow_particle(), ChuteBottom::setup_particles_initial_conditions(), and test().

33 {
34 
35 switch (type)
36  {
37  case 0:
38  return get_LCG(min,max);
39  break;
40  case 1:
41  return get_LFG(min,max);
42  break;
43  }
44 //end switch
45 
46 
47 
48 return get_LFG(min,max);
49 
50 }
Mdouble get_LCG(Mdouble min, Mdouble max)
This is a basic Linear Congruential Generator Random.
Definition: RNG.cc:55
Mdouble get_LFG(Mdouble min, Mdouble max)
This is a Laggend Fibonacci Generator.
Definition: RNG.cc:89
int type
This is the type of random number generator.
Definition: RNG.h:94
void RNG::randomise ( )
inline

sets the random variables such that they differ for each run

Definition at line 59 of file RNG.h.

References set_RandomSeed().

Referenced by MD::readNextArgument().

59 {set_RandomSeed(time(NULL));}
void set_RandomSeed(Mdouble new_seed)
This is the seed for the random number generator.
Definition: RNG.h:46
void RNG::seed_LFG ( )
private

This seed the LFG.

Definition at line 77 of file RNG.cc.

References get_LCG(), p, and random_seed_LFG.

Referenced by RNG(), set_LFGParms(), and set_RandomSeed().

78 {
79  for (int i=0;i<p;i++)
80  {
81  random_seed_LFG[i]=get_LCG(0,1.0);
82  }
83 }
Mdouble get_LCG(Mdouble min, Mdouble max)
This is a basic Linear Congruential Generator Random.
Definition: RNG.cc:55
std::vector< Mdouble > random_seed_LFG
This is the seeds required for the LFG.
Definition: RNG.h:85
long int p
This are the parameters that control the LFG random generator.
Definition: RNG.h:91
void RNG::set_LCGParms ( int  new_a,
int  new_c,
int  new_m 
)
inline

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

Definition at line 55 of file RNG.h.

References a, c, and m.

56  {a=new_a;c=new_c;m=new_m;}
long int a
This are the two parameters that control the LCG random generated.
Definition: RNG.h:88
long int c
Definition: RNG.h:88
long int m
Definition: RNG.h:88
void RNG::set_LFGParms ( int  new_p,
int  new_q 
)

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

Definition at line 172 of file RNG.cc.

References p, q, random_seed_LFG, and seed_LFG().

173 {
174  //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.
175  if (new_p > new_q)
176  {
177  p=new_p;
178  q=new_q;
179  }
180  else
181  {
182  p=new_q;
183  q=new_p;
184  }
185 
186  random_seed_LFG.resize(p);
187  seed_LFG();
188 
189 }
void seed_LFG()
This seed the LFG.
Definition: RNG.cc:77
long int q
Definition: RNG.h:91
std::vector< Mdouble > random_seed_LFG
This is the seeds required for the LFG.
Definition: RNG.h:85
long int p
This are the parameters that control the LFG random generator.
Definition: RNG.h:91
void RNG::set_RandomSeed ( Mdouble  new_seed)
inline

This is the seed for the random number generator.

Definition at line 46 of file RNG.h.

References random_seed_LCG, and seed_LFG().

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

46 {random_seed_LCG=new_seed; seed_LFG();}
void seed_LFG()
This seed the LFG.
Definition: RNG.cc:77
unsigned long int random_seed_LCG
This is the initiall seed of the RNG.
Definition: RNG.h:82
void RNG::set_RNtypeLCG ( )
inline

Make the random number generator based on LCG.

Definition at line 65 of file RNG.h.

References type.

65 {type=0;}
int type
This is the type of random number generator.
Definition: RNG.h:94
void RNG::set_RNtypeLFG ( )
inline

Make the random number generator based on LFG.

Definition at line 68 of file RNG.h.

References type.

68 {type=1;}
int type
This is the type of random number generator.
Definition: RNG.h:94
Mdouble RNG::test ( )

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

It reports a probabity 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 117 of file RNG.cc.

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

118 {
119  //This are the fixed parameters that define the test
120  static int num_of_tests=100000;
121  static Mdouble max_num=100.0;
122  static int num_of_bins=10;
123 
124  //This is the generated random_number
125  Mdouble rn;
126  //This is the bin the random number will lie in
127  int bin=0;
128  //This is a vector of bins
129  std::vector<int> count;
130  count.resize(num_of_bins);
131 
132  //Initialisation of the bins
133  for (int i=0;i<num_of_bins;i++)
134  {
135  count[bin]=0;
136  }
137 
138 
139 
140  //Loop over a number of tests
141  for (int i=0;i<num_of_tests;i++)
142  {
143  rn=get_RN(0.0,max_num);
144  bin=floor(rn*num_of_bins/max_num);
145 
146 
147  //Add one to the bin count
148  count[bin]++;
149 
150  }
151 
152 
153 
154 
155 
156  //Final post-process the result and report on the random number
157  Mdouble chi_cum=0.0;
158  Mdouble expected=num_of_tests/num_of_bins;
159 
160  for (int i=0;i<num_of_bins;i++)
161  {
162  chi_cum=chi_cum+(count[i]-expected)*(count[i]-expected)/expected;
163  std::cout << i << " : " << count[i] << " : " <<(count[i]-expected)*(count[i]-expected)/expected << std::endl;
164  }
165  //end for loop over comuputing the chi-squared value.
166  std::cout << chi_cum << std::endl;
167 
168  return mathsFunc::chi_squared_prob(chi_cum,num_of_bins);
169 }
Mdouble get_RN(Mdouble min, Mdouble max)
This is a random generating routine can be used for initial positions.
Definition: RNG.cc:32
double Mdouble
Definition: ExtendedMath.h:33
Mdouble chi_squared_prob(Mdouble x, int k)
This is the function which actually gives the probability back using a chi squared test...
Definition: ExtendedMath.cc:78

Member Data Documentation

long int RNG::a
private

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

Definition at line 88 of file RNG.h.

Referenced by get_LCG(), RNG(), and set_LCGParms().

long int RNG::c
private

Definition at line 88 of file RNG.h.

Referenced by get_LCG(), RNG(), and set_LCGParms().

long int RNG::m
private

Definition at line 88 of file RNG.h.

Referenced by get_LCG(), RNG(), and set_LCGParms().

long int RNG::p
private

This are the parameters that control the LFG random generator.

Definition at line 91 of file RNG.h.

Referenced by get_LFG(), RNG(), seed_LFG(), and set_LFGParms().

long int RNG::q
private

Definition at line 91 of file RNG.h.

Referenced by get_LFG(), RNG(), and set_LFGParms().

unsigned long int RNG::random_seed_LCG
private

This is the initiall seed of the RNG.

Definition at line 82 of file RNG.h.

Referenced by get_LCG(), RNG(), and set_RandomSeed().

std::vector<Mdouble> RNG::random_seed_LFG
private

This is the seeds required for the LFG.

Definition at line 85 of file RNG.h.

Referenced by get_LFG(), RNG(), seed_LFG(), and set_LFGParms().

int RNG::type
private

This is the type of random number generator.

Definition at line 94 of file RNG.h.

Referenced by get_RN(), RNG(), set_RNtypeLCG(), and set_RNtypeLFG().


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