OSFEG-C++  1.4.6
OpenSF Error Generation Library
Random.h
1 // Random.h: Definition and Implementation of Random Number Distribution Class
2 // Ref: Richard Saucier, "Computer Generation of Statistical
3 // Distributions," ARL-TR-2168, US Army Research Laboratory,
4 // Aberdeen Proving Ground, MD, 21005-5068, March 2000.
5 
6 #ifndef RANDOM_H
7 #define RANDOM_H
8 
9 #include <array>
10 #include <cstdint> // for int32_t and its limits
11 using namespace std; // NOTE: to be removed!
12 
13 // Constants for Tausworthe random bit generator
14 // Ref: Tausworthe, Robert C., "Random Numbers Generated by Linear Recurrence
15 // Modulo Two," Mathematics of Computation, vol. 19, pp. 201-209, 1965.
16 
17 static const unsigned DEGREE_MAX = 32; // max degree (bits per word)
18 
19 static const unsigned BIT[ 1 + DEGREE_MAX ] = {
20 
21 // Hexadecimal Value Degree
22 // ----------- ----- ------
23  0x00000000, // 0 0
24  0x00000001, // 2^0 1
25  0x00000002, // 2^1 2
26  0x00000004, // 2^2 3
27  0x00000008, // 2^3 4
28  0x00000010, // 2^4 5
29  0x00000020, // 2^5 6
30  0x00000040, // 2^6 7
31  0x00000080, // 2^7 8
32  0x00000100, // 2^8 9
33  0x00000200, // 2^9 10
34  0x00000400, // 2^10 11
35  0x00000800, // 2^11 12
36  0x00001000, // 2^12 13
37  0x00002000, // 2^13 14
38  0x00004000, // 2^14 15
39  0x00008000, // 2^15 16
40  0x00010000, // 2^16 17
41  0x00020000, // 2^17 18
42  0x00040000, // 2^18 19
43  0x00080000, // 2^19 20
44  0x00100000, // 2^20 21
45  0x00200000, // 2^21 22
46  0x00400000, // 2^22 23
47  0x00800000, // 2^23 24
48  0x01000000, // 2^24 25
49  0x02000000, // 2^25 26
50  0x04000000, // 2^26 27
51  0x08000000, // 2^27 28
52  0x10000000, // 2^28 29
53  0x20000000, // 2^29 30
54  0x40000000, // 2^30 31
55  0x80000000 // 2^31 32
56 };
57 
58 // Coefficients that define a primitive polynomial (mod 2)
59 // Ref: Watson, E. J., "Primitive Polynomials (Mod 2),"
60 // Mathematics of Computation, vol. 16, pp. 368-369, 1962.
61 
62 static const unsigned MASK[ 1 + DEGREE_MAX ] = {
63 
64  BIT[0], // 0
65  BIT[0], // 1
66  BIT[1], // 2
67  BIT[1], // 3
68  BIT[1], // 4
69  BIT[2], // 5
70  BIT[1], // 6
71  BIT[1], // 7
72  BIT[4] + BIT[3] + BIT[2], // 8
73  BIT[4], // 9
74  BIT[3], // 10
75  BIT[2], // 11
76  BIT[6] + BIT[4] + BIT[1], // 12
77  BIT[4] + BIT[3] + BIT[1], // 13
78  BIT[5] + BIT[3] + BIT[1], // 14
79  BIT[1], // 15
80  BIT[5] + BIT[3] + BIT[2], // 16
81  BIT[3], // 17
82  BIT[5] + BIT[2] + BIT[1], // 18
83  BIT[5] + BIT[2] + BIT[1], // 19
84  BIT[3], // 20
85  BIT[2], // 21
86  BIT[1], // 22
87  BIT[5], // 23
88  BIT[4] + BIT[3] + BIT[1], // 24
89  BIT[3], // 25
90  BIT[6] + BIT[2] + BIT[1], // 26
91  BIT[5] + BIT[2] + BIT[1], // 27
92  BIT[3], // 28
93  BIT[2], // 29
94  BIT[6] + BIT[4] + BIT[1], // 30
95  BIT[3], // 31
96  BIT[7] + BIT[5] + BIT[3] + BIT[2] + BIT[1] // 32
97 };
98 
99 
100 
109 class Random {
110 
111 public:
112  // overloaded relational operators
114  friend bool operator==(const Random& a, const Random& b);
116  inline friend bool operator!=(const Random& a, const Random& b) { return !(a == b); }
117 
118  Random( int32_t seed );
119  Random( );
120  Random( int32_t seed, bool f );
121  Random( const Random& r ) = default;
122  Random& operator=( const Random& r ) = default;
123 
126  void reset( int32_t seed );
127  void reset( );
132  double beta( double v, double w,
133  double xMin = 0., double xMax = 1. );
134  double gamma( double a, double b, double c );
137  double exponential( double a = 0., double c = 1. );
139  double normal( double mu = 0., double sigma = 1. );
140 
141  double uniform( double xMin = 0., double xMax = 1. );
146  int poisson( double mu );
147  int uniformDiscrete( int i, int j );
149 
151  int32_t getSeed();
152 
153 private:
154 
155 /* Variables used in normalModified function */
156  struct {
157  bool f;
158  double pR2;
159  std::array<double,2> pXY;
160  } _gauss;
161 
162  static constexpr int32_t _M = 0x7fffffff; // 2147483647 (Mersenne prime 2^31-1)
163  static constexpr int32_t A_ = 0x10ff5; // 69621
164  static constexpr int32_t Q_ = 0x787d; // 30845
165  static constexpr int32_t R_ = 0x5d5e; // 23902
166  static constexpr double _F = 1. / _M;
167  static constexpr short _NTAB = 32; // arbitrary length of shuffle table
168  static constexpr int32_t _DIV = 1+(_M-1)/_NTAB;
169  int32_t _table[ _NTAB ]; // shuffle table of seeds
170  int32_t _next; // seed to be used as index into table
171  int32_t _seed; // current random number seed
172  unsigned _seed2; // seed for tausworthe random bit
173 
174 
175  void _seedTable( ); // seeds the shuffle table
176  double _u( ); // uniform rng
177 
178  double normalModified( double mu = 0., // Normal
179  double sigma = 1. );
180 };
181 
182 #endif
Random numbers engine with additional distributions.
Definition: Random.h:109
Random(const Random &r)=default
copy constructor (copies current state)
double beta(double v, double w, double xMin=0., double xMax=1.)
Beta (v > 0. and w > 0.)
double pR2
cache for the squared norm of pXY
Definition: Random.h:158
int32_t getSeed()
Return the current seed value.
double normal(double mu=0., double sigma=1.)
Normal (Gaussian)
bool f
true if new uniform variates need to be pulled, false if the data here is usable
Definition: Random.h:157
Random(int32_t seed)
constructor to set the seed
void reset()
reset seed from current process id
friend bool operator==(const Random &a, const Random &b)
Compare internal state of generators
double uniform(double xMin=0., double xMax=1.)
Uniform on [xMin,xMax)
double exponential(double a=0., double c=1.)
int uniformDiscrete(int i, int j)
Random(int32_t seed, bool f)
constructor to set the seed and the modified state
Random()
constructor to autogenerate a seed
void reset(int32_t seed)
reset the seed explicitly
double gamma(double a, double b, double c)
friend bool operator!=(const Random &a, const Random &b)
Compare internal state of generators
Definition: Random.h:116
Random & operator=(const Random &r)=default
overloaded assignment (replaces state)
int poisson(double mu)
Poisson.