MercuryDPM  0.10
 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 "ExtendedMath.h"
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 }
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 }
70 
71 
72 /**************************************
73  * This sets the seed for LFG using LCG
74  *
75  *
76  **************************************/
78 {
79  for (int i=0;i<p;i++)
80  {
81  random_seed_LFG[i]=get_LCG(0,1.0);
82  }
83 }
84 
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 }
110 
111 
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 }
170 
172 void RNG::set_LFGParms(int new_p, int new_q)
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 set_LFGParms(int new_p, int new_q)
This function sets the parametes for the LFG random number generator.
Definition: RNG.cc:172
long int a
This are the two parameters that control the LCG random generated.
Definition: RNG.h:88
Mdouble get_LCG(Mdouble min, Mdouble max)
This is a basic Linear Congruential Generator Random.
Definition: RNG.cc:55
Mdouble get_RN(Mdouble min, Mdouble max)
This is a random generating routine can be used for initial positions.
Definition: RNG.cc:32
long int c
Definition: RNG.h:88
long int m
Definition: RNG.h:88
Mdouble get_LFG(Mdouble min, Mdouble max)
This is a Laggend Fibonacci Generator.
Definition: RNG.cc:89
void seed_LFG()
This seed the LFG.
Definition: RNG.cc:77
double Mdouble
Definition: ExtendedMath.h:33
int type
This is the type of random number generator.
Definition: RNG.h:94
long int q
Definition: RNG.h:91
Mdouble test()
This function tests the quality of random numbers, based on the chi-squared test. ...
Definition: RNG.cc:117
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
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