forked from dds-bridge/ddd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rng.h
277 lines (221 loc) · 7.05 KB
/
rng.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// =============================================================================
/* rng.cpp RNG - random number generators
PMC 14-jun-2005
PMC 10-jul-2005
PMC 13-Jul-2006 updated, default is now MT
Copyright 2005-2006 P.M.Cronje
RNG is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
RNG is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RNG; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// =============================================================================
#ifndef RNG_H
#define RNG_H
typedef unsigned long long uint64;
// -----------------------------------------------------------------------------
// Entropy test
// -----------------------------------------------------------------------------
void initEnt(bool binmode);
void addEnt(unsigned char * buf, int buflen);
void endEnt(double * r_ent, double * r_chisq, double * r_mean,
double * r_montepicalc, double * r_scc);
void prtEnt(double r_chisq, double r_mean,
double r_montepicalc, double r_scc);
// -----------------------------------------------------------------------------
// Available RNG's
// -----------------------------------------------------------------------------
enum eRNG
{
eRNG_QD1 = 0, // 'Quick and dirty' LCG
eRNG_MT = 1, // Mersenne Twister MT19937_02 (default)
eRNG_MTHR = 2, // 'Mother of all RNG' (Marsaglia)
eRNG_WELL = 3, // WELL1024a, single seed with run-in
eRNG_COUNT = 4
}; // enum eRNG
// -----------------------------------------------------------------------------
// Description
// -----------------------------------------------------------------------------
//
// The QD1 is fast, but is only as good as an LCG can be,
// it should only be used where true randomness is not required.
// LCG = linear congruential generator.
//
// The other (mt/mthr/well) have been selected after many tests
// using the TestU01 software of Ecuyer and Simard, and pass most
// of the stringent tests for random number generators, failing only
// occasionally at a significance level of a few parts in 0.001.
// They all have extremely long periods and are recommended for any
// serious simulation work requiring many millions of random numbers.
//
// The default generator is MT19937_02.
//
// The acronym WELL means 'Well Equidistributed Long-period Linear'
// The generator WELL1024a has been modified to:
// - initialize with a single seed
// - generate equidistributed initial values
// - run for 50,000 numbers to escape from the initial setup
//
// The generators mt/mthr are initialized from a single seed using a WELL.
//
// Speed (relative elapsed time for 10,000,000 numbers)
// qd1 1.00
// mt 1.25
// mthr 1.90
// well 1.41
//
// -----------------------------------------------------------------------------
extern const char * pszRNGGen[eRNG_COUNT];
extern const char * pszRNGGenList;
extern const char * pszRNGGenDefault;
enum eRNG const eRNGGenDefault = eRNG_MT;
// -----------------------------------------------------------------------------
// forward declaration of derived classes
// -----------------------------------------------------------------------------
class cRNG_QD1;
class cRNG_WELL;
class cRNG_Mother;
class cRNG_MT19937;
// -----------------------------------------------------------------------------
// cRNG - base class
// -----------------------------------------------------------------------------
class cRNG
{
public:
virtual ~cRNG()
{ }
// set seed
virtual void set(unsigned int useed) = 0;
// generator name
virtual char * getszRandom() = 0;
// next random number
virtual unsigned int random() = 0;
// return random unsigned int, where 0 <= unsigned int < uirange
virtual unsigned int randomUint(unsigned int urange);
// creates an instance of cRNG,
// specifying either eRNG or a string identifier pszrng,
// if pszgen incorrectly specified creates a cRNG_MT
//
// erng pszrng
// --------- --------
// eRNG_QD1 "qd1"
// eRNG_MT "mt"
// eRNG_MTHR "mthr"
// eRNG_WELL "well"
//
// note: the object must be deleted after use
static cRNG * createRNG(eRNG erng, unsigned int useed);
static cRNG * createRNG(char * pszrng, unsigned int useed);
enum eRNG getRNG()
{
return RNG;
}
const char * getpszGen()
{
return static_cast<const char *>(szGen);
}
protected:
eRNG RNG;
char szGen[16];
unsigned int uSeed;
}; // cRNG
// -----------------------------------------------------------------------------
class cRNG_QD1 : public cRNG
{
public:
cRNG_QD1(unsigned int useed = 0)
{
set(useed);
}
// set
virtual void set(unsigned int useed);
// generator name
virtual char * getszRandom()
{
return strdup("QD1");
}
// next random number
virtual
unsigned int random();
unsigned int getSeed()
{
return uSeed;
}
protected:
}; // cRNG_QD1
// -----------------------------------------------------------------------------
class cRNG_WELL : public cRNG
{
public:
cRNG_WELL(unsigned int useed = 0)
{
set(useed);
}
// set
virtual void set(unsigned int useed);
// generator name
virtual char * getszRandom()
{
return strdup("WELL1024u");
}
// next random number
virtual
unsigned int random();
protected:
unsigned int state_i, STATE[32], z0, z1, z2;
}; // cRNG_WELL
// -----------------------------------------------------------------------------
class cRNG_Mother : public cRNG
{
public:
cRNG_Mother(unsigned int useed = 0)
{
set(useed);
}
// set
virtual void set(unsigned int useed);
// generator name
virtual char * getszRandom()
{
return strdup("Mother");
}
// next random number
virtual
unsigned int random();
protected:
unsigned int smthr[4];
uint64 xm4, xm3, xm2, xm1, mcarry;
}; // cRNG_Mother
// -----------------------------------------------------------------------------
class cRNG_MT19937 : public cRNG
{
public:
cRNG_MT19937(unsigned int useed = 0)
{
set(useed);
}
// set
virtual void set(unsigned int useed);
// generator name
virtual char * getszRandom()
{
return strdup("MT19937");
}
// next random number
virtual
unsigned int random();
protected:
unsigned int mti, U[624];
unsigned long mag01[2];
void initBySeed(unsigned int useed);
void initByArray(unsigned int lenkey, unsigned int key[]);
}; // cRNG_MT19937
// -----------------------------------------------------------------------------
#endif