MercuryDPM  Beta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RNG.cc
Go to the documentation of this file.
1 //Copyright (c) 2013-2014, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #include "RNG.h"
34 {
36  a_ = 1103515245;
37  c_ = 12345;
38  m_ = 1024 * 1024 * 1024;
40  p_ = 607;
41  q_ = 273;
44 }
45 
46 void RNG::setRandomSeed(unsigned long int new_seed)
47 {
50 }
51 
52 void RNG::setLinearCongruentialGeneratorParmeters(unsigned const int a, unsigned const int c, unsigned const int m)
53 {
54  a_ = a;
55  c_ = c;
56  m_ = m;
57 }
58 
60 {
61  setRandomSeed(static_cast<unsigned long int>(time(nullptr)));
62 }
63 
65 {
66  type=type_;
67 }
68 
70 {
71  switch (type_)
72  {
77  }
78 }
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 }
97 
98 /**************************************
99  * This sets the seed for LFG using LCG
100  *
101  *
102  **************************************/
104 {
105  for (unsigned int i = 0; i < p_; i++)
106  {
108  }
109 }
110 
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 }
134 
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 }
186 
188 void RNG::setLaggedFibonacciGeneratorParameters(const unsigned int p, const unsigned int q)
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 }
RNGType
Definition: RNG.h:38
unsigned long int c_
Definition: RNG.h:126
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
Mdouble getRandomNumberFromLaggedFibonacciGenerator(Mdouble min, Mdouble max)
This is a Lagged Fibonacci Generator.
Definition: RNG.cc:115
void seedLaggedFibonacciGenerator()
This seed the LFG.
Definition: RNG.cc:103
unsigned long int q_
Definition: RNG.h:131
double Mdouble
void setLaggedFibonacciGeneratorParameters(const unsigned int p, const unsigned int q)
This function sets the parameters for the LFG random number generator.
Definition: RNG.cc:188
void randomise()
sets the random variables such that they differ for each run
Definition: RNG.cc:59
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.
Definition: RNG.cc:52
Mdouble getRandomNumberFromLinearCongruentialGenerator(Mdouble min, Mdouble max)
This is a basic Linear Congruential Generator Random.
Definition: RNG.cc:83
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
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
Mdouble test()
This function tests the quality of random numbers, based on the chi-squared test. ...
Definition: RNG.cc:140
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
void setRandomNumberGenerator(RNGType type)
Allows the user to set which random number generator is used.
Definition: RNG.cc:64
RNGType type_
This is the type of random number generator.
Definition: RNG.h:136
RNG()
default constructor
Definition: RNG.cc:33
Mdouble getRandomNumber(Mdouble min, Mdouble max)
This is a random generating routine can be used for initial positions.
Definition: RNG.cc:69
unsigned long int m_
Definition: RNG.h:126