MercuryDPM  Alpha
 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::read(std::istream& is)
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 }
70 
71 void RNG::write(std::ostream& os) const
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 }
83 
84 void RNG::setLinearCongruentialGeneratorParmeters(unsigned const int a, unsigned const int c, unsigned const int m)
85 {
86  a_ = a;
87  c_ = c;
88  m_ = m;
89 }
90 
92 {
93  setRandomSeed(static_cast<unsigned long int>(time(nullptr)));
94 }
95 
97 {
98  type=type_;
99 }
100 
102 {
103  switch (type_)
104  {
109  }
110 }
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 }
129 
130 /**************************************
131  * This sets the seed for LFG using LCG
132  *
133  *
134  **************************************/
136 {
137  for (unsigned int i = 0; i < p_; i++)
138  {
140  }
141 }
142 
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 }
166 
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 }
218 
220 void RNG::setLaggedFibonacciGeneratorParameters(const unsigned int p, const unsigned int q)
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 read(std::istream &is)
Definition: RNG.cc:52
unsigned long int c_
Definition: RNG.h:140
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...
Mdouble getRandomNumberFromLaggedFibonacciGenerator(Mdouble min, Mdouble max)
This is a Lagged Fibonacci Generator.
Definition: RNG.cc:147
void seedLaggedFibonacciGenerator()
This seed the LFG.
Definition: RNG.cc:135
unsigned long int q_
Definition: RNG.h:145
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:220
void randomise()
sets the random variables such that they differ for each run
Definition: RNG.cc:91
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:84
void write(std::ostream &os) const
Definition: RNG.cc:71
Mdouble getRandomNumberFromLinearCongruentialGenerator(Mdouble min, Mdouble max)
This is a basic Linear Congruential Generator Random.
Definition: RNG.cc:115
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
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:172
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
void setRandomNumberGenerator(RNGType type)
Allows the user to set which random number generator is used.
Definition: RNG.cc:96
RNGType
Definition: RNG.h:38
RNGType type_
This is the type of random number generator.
Definition: RNG.h:150
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:101
unsigned long int m_
Definition: RNG.h:140