RNG.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2023, 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 #ifndef MECURYDPM_RNG_H
27 #define MECURYDPM_RNG_H
28 
29 //Used by the randomise function
30 #include <ctime>
31 #include <cmath>
32 #include <vector>
33 #include "ExtendedMath.h"
34 
38 enum class RNGType : unsigned char
39 {
42 };
43 
52 class RNG
53 {
54 public:
58  RNG();
59 
64  void setRandomSeed(unsigned long int new_seed);
65 
66  void read(std::istream& is);
67 
68  void write(std::ostream& os) const;
69 
74 
76 
81  {
82  return getRandomNumber(min, max);
83  }
84 
85  /*
86  * \brief Shorthand for getRandomNumber()
87  */
89  {
90  return getRandomNumber(0.0, 1.0);
91  }
92 
98 
103 
107  unsigned int getPoissonVariate(Mdouble lambda);
108 
112  Mdouble test();
113 
117  void setLinearCongruentialGeneratorParmeters(const unsigned int a, const unsigned int c, unsigned int m);
118 
122  void randomise();
123 
127  void setLaggedFibonacciGeneratorParameters(const unsigned int p, const unsigned int q);
128 
133 
134 private:
135 
140 
145 
150 
155 
160 
164  unsigned long int a_, c_, m_;
165 
169  unsigned long int p_, q_;
170 
175 
176  /*
177  * \todo JMFT: These Box--Muller private variables are currently not
178  * read/write from .restart files. This leads to undefined (unrepeatable)
179  * behaviour when restarting if you use normal variates.
180  */
181 
186 
191 
192 };
193 
194 #endif
double Mdouble
Definition: GeneralDefine.h:34
RNGType
Definition: RNG.h:39
@ LAGGED_FIBONACCI_GENERATOR
@ LINEAR_CONGRUENTIAL_GENERATOR
This is a class that generates random numbers i.e. named the Random Number Generator (RNG).
Definition: RNG.h:53
RNG()
default constructor
Definition: RNG.cc:36
unsigned long int m_
Definition: RNG.h:164
unsigned long int q_
Definition: RNG.h:169
unsigned int getPoissonVariate(Mdouble lambda)
Produces a random number according to a Poisson distribution.
Definition: RNG.cc:212
void read(std::istream &is)
Definition: RNG.cc:59
Mdouble savedBoxMuller_
A storage space for the so-far-unused variate from the pair generated by Box–Muller.
Definition: RNG.h:190
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 i...
Definition: RNG.cc:53
Mdouble getRandomNumberFromLinearCongruentialGenerator(Mdouble min, Mdouble max)
This is a basic Linear Congruential Generator Random.
Definition: RNG.cc:234
unsigned long int randomSeedLinearCongruentialGenerator_
This is the initial seed of the RNG.
Definition: RNG.h:154
void setLinearCongruentialGeneratorParmeters(const unsigned int a, const unsigned int c, unsigned int m)
This functions set the parameters for the LCG random number generator. It goes multiplier,...
Definition: RNG.cc:91
unsigned long int c_
Definition: RNG.h:164
Mdouble test()
This function tests the quality of random numbers, based on the chi-squared test.
Definition: RNG.cc:289
void write(std::ostream &os) const
Definition: RNG.cc:78
RNGType type_
This is the type of random number generator.
Definition: RNG.h:174
unsigned long int a_
This are the two parameters that control the LCG random generated.
Definition: RNG.h:164
bool haveSavedBoxMuller_
A flag that keeps track of whether or not to generate a new pair of normal variates (using Box–Muller...
Definition: RNG.h:185
Mdouble getNormalVariate()
Produces a random number according to a normal distribution with mean 0 and standard deviation 1.
Definition: RNG.cc:164
Mdouble getRandomNumber()
This is a random generating routine can be used for initial positions.
Definition: RNG.cc:143
Mdouble operator()()
Definition: RNG.h:88
void seedLaggedFibonacciGenerator()
This seed the LFG.
Definition: RNG.cc:253
unsigned long int p_
This are the parameters that control the LFG random generator.
Definition: RNG.h:169
void setLaggedFibonacciGeneratorParameters(const unsigned int p, const unsigned int q)
This function sets the parameters for the LFG random number generator.
Definition: RNG.cc:338
Mdouble getRandomNumberFromLaggedFibonacciGenerator(Mdouble min, Mdouble max)
This is a Lagged Fibonacci Generator.
Definition: RNG.cc:265
std::vector< Mdouble > randomSeedLaggedFibonacciGenerator_
This is the seeds required for the LFG.
Definition: RNG.h:159
void randomise()
sets the random variables such that they differ for each run
Definition: RNG.cc:98
Mdouble operator()(Mdouble min, Mdouble max)
Shorthand for getRandomNumber(min, max)
Definition: RNG.h:80
void setRandomNumberGenerator(RNGType type)
Allows the user to set which random number generator is used.
Definition: RNG.cc:138