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