iLand
SimpleRNG.h
Go to the documentation of this file.
1#ifndef SIMPLERNG_H
2#define SIMPLERNG_H
3
4// A simple random number generator based on George Marsaglia's MWC (Multiply With Carry) generator.
5// This is not intended to take the place of the library's primary generator, Mersenne Twister.
6// Its primary benefit is that it is simple to extract its state.
7
8// Source: http://www.johndcook.com/cpp_random_number_generation.html
10{
11public:
12
13 SimpleRNG();
14
15 // Seed the random number generator
16 void SetState(unsigned int u, unsigned int v);
17
18 // Extract the internal state of the generator
19 void GetState(unsigned int& u, unsigned int& v);
20
21 // A uniform random sample from the open interval (0, 1)
22 double GetUniform();
23
24 // A uniform random sample from the set of unsigned integers
25 unsigned int GetUint();
26
27 // This stateless version makes it more convenient to get a uniform
28 // random value and transfer the state in and out in one operation.
29 double GetUniform(unsigned int& u, unsigned int& v);
30
31 // This stateless version makes it more convenient to get a random unsigned integer
32 // and transfer the state in and out in one operation.
33 unsigned int GetUint(unsigned int& u, unsigned int& v);
34
35 // Normal (Gaussian) random sample
36 double GetNormal(double mean, double standardDeviation);
37
38 // Exponential random sample
39 double GetExponential(double mean);
40
41 // Gamma random sample
42 double GetGamma(double shape, double scale);
43
44 // Chi-square sample
45 double GetChiSquare(double degreesOfFreedom);
46
47 // Inverse-gamma sample
48 double GetInverseGamma(double shape, double scale);
49
50 // Weibull sample
51 double GetWeibull(double shape, double scale);
52
53 // Cauchy sample
54 double GetCauchy(double median, double scale);
55
56 // Student-t sample
57 double GetStudentT(double degreesOfFreedom);
58
59 // The Laplace distribution is also known as the double exponential distribution.
60 double GetLaplace(double mean, double scale);
61
62 // Log-normal sample
63 double GetLogNormal(double mu, double sigma);
64
65 // Beta sample
66 double GetBeta(double a, double b);
67
68 // Poisson sample
69 int GetPoisson(double lambda);
70
71private:
72 unsigned int m_u, m_v;
73 int PoissonLarge(double lambda);
74 int PoissonSmall(double lambda);
75 double LogFactorial(int n);
76};
77
78
79#endif
Definition: SimpleRNG.h:10
void SetState(unsigned int u, unsigned int v)
Definition: SimpleRNG.cpp:17
double GetChiSquare(double degreesOfFreedom)
Definition: SimpleRNG.cpp:127
double GetLaplace(double mean, double scale)
Definition: SimpleRNG.cpp:186
void GetState(unsigned int &u, unsigned int &v)
Definition: SimpleRNG.cpp:23
double GetInverseGamma(double shape, double scale)
Definition: SimpleRNG.cpp:134
double GetNormal(double mean, double standardDeviation)
Definition: SimpleRNG.cpp:55
unsigned int GetUint()
Definition: SimpleRNG.cpp:49
double GetStudentT(double degreesOfFreedom)
Definition: SimpleRNG.cpp:169
double GetWeibull(double shape, double scale)
Definition: SimpleRNG.cpp:141
SimpleRNG()
Definition: SimpleRNG.cpp:9
double GetLogNormal(double mu, double sigma)
Definition: SimpleRNG.cpp:194
double GetGamma(double shape, double scale)
Definition: SimpleRNG.cpp:85
double GetBeta(double a, double b)
Definition: SimpleRNG.cpp:199
double GetExponential(double mean)
Definition: SimpleRNG.cpp:73
double GetCauchy(double median, double scale)
Definition: SimpleRNG.cpp:153
double GetUniform()
Definition: SimpleRNG.cpp:44
int GetPoisson(double lambda)
Definition: SimpleRNG.cpp:219