-
Notifications
You must be signed in to change notification settings - Fork 0
/
MT_random.c
379 lines (323 loc) · 12 KB
/
MT_random.c
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "MT_random.h"
#define INT_STATELENGTH (1+2*9+312*17+1)
#define UNIFORM_STATELENGTH (1+2*17+1+INT_STATELENGTH)
#define GAUSS_STATELENGTH (1+4*17+9+1+(UNIFORM_STATELENGTH))
#define NN (MTSTATESIZE)
#define MM 0x000000000000009C
#define UM 0xFFFFFFFF80000000
#define LM 0x000000007FFFFFFF
#define INV2POW53MIN1 (1.0/((double)0x001FFFFFFFFFFFFF))
const long int mag[2] = {0x0000000000000000, 0xB5026F5AA96619E9} ;
const double twopi = 6.28318530717958647692528676655900559 ;
/////////////////////////////////////////////////////////////////////////////
// Description:
// Initializes a int random number generator
// Variables:
// seed: The seed
// Return value:
// rng_int: int random number state
/////////////////////////////////////////////////////////////////////////////
rng_int_type init_rng_int(int seed){
rng_int_type rng_int;
rng_int.seed = seed;
rng_int.mtstate[0] = seed ;
for (int i=1; i<NN; i++){
rng_int.mtstate[i] = 0x5851F42D4C957F2D *
(rng_int.mtstate[i-1]^(((unsigned long int)rng_int.mtstate[i-1])>>62)) + i;
}
rng_int.mtidx = NN+1;
return rng_int;
}
/////////////////////////////////////////////////////////////////////////////
// Description:
// Initializes a uniform random number generator
// Variables:
// seed: The seed
// lower: lower bound of random numbers
// upper: upper bound of random numbers
// Return value:
// rng_uniform: uniform random number state
/////////////////////////////////////////////////////////////////////////////
rng_uniform_type init_rng_uniform(int seed, double lower, double upper){
rng_uniform_type rng_uniform;
rng_uniform.rng_int = init_rng_int(seed);
rng_uniform.lower = lower;
rng_uniform.upper = upper;
return rng_uniform;
}
/////////////////////////////////////////////////////////////////////////////
// Description:
// Initializes a gaussian random number generator
// Variables:
// seed: The seed
// mean: Mean value of the gaussian
// stddev: standard deviation of the gaussian
// Return value:
// rng_gaussian: gaussian random number state
/////////////////////////////////////////////////////////////////////////////
rng_gaussian_type init_rng_gaussian(int seed, double mean, double stddev){
rng_gaussian_type rng_gaussian;
rng_gaussian.rng_uniform = init_rng_uniform(seed, 0.0, 1.0);
rng_gaussian.mean = mean;
rng_gaussian.stddev = stddev;
rng_gaussian.z1 = 0.0;
rng_gaussian.z2 = 0.0;
rng_gaussian.generated = false;
return rng_gaussian;
}
/////////////////////////////////////////////////////////////////////////////
// Description:
// Generates the next int random number
// Variables:
// rng_int: Random number generator state
// Return value:
// randnum: next random number
/////////////////////////////////////////////////////////////////////////////
long int next_random_lint(rng_int_type* rng_int){
unsigned long int rng = 0;
// if pool of random numbers is drained create a set of new ones
if (rng_int->mtidx > NN-2){
for (int i=0; i<NN-MM; i++) {
rng = (rng_int->mtstate[i] & UM) ^ (rng_int->mtstate[i+1] & LM);
rng_int->mtstate[i] = (rng_int->mtstate[i+MM] ^ (rng>>1)) ^ (mag[(rng& 1)]) ;
}
for (int i=NN-MM; i<NN-1; i++) {
rng = (rng_int->mtstate[i] & UM) ^ (rng_int->mtstate[i+1] & LM);
rng_int->mtstate[i] = (rng_int->mtstate[i+MM-NN] ^ (rng>>1)) ^ (mag[(rng& 1)]) ;
}
rng = (rng_int->mtstate[NN-1] & UM) ^ (rng_int->mtstate[0] & LM);
rng_int->mtstate[NN-1] = (rng_int->mtstate[MM-1] ^ (rng>>1)) ^ (mag[(rng& 1)]) ;
rng_int->mtidx = -1 ;
}
// get a number from stack and mix it up to produce the final random number
rng_int->mtidx += 1 ;
rng = rng_int->mtstate[rng_int->mtidx] ;
rng = (rng) ^ ((rng>>29) & 0x5555555555555555) ;
rng = (rng) ^ ((rng<<17) & 0x71d67fffeda60000) ;
rng = (rng) ^ ((rng<<37) & 0xfff7eee000000000) ;
rng = (rng) ^ (rng>>43) ;
return (long int) rng;
}
/////////////////////////////////////////////////////////////////////////////
// Description:
// Generates the next int random number
// Variables:
// rng_int: Random number generator state
// Return value:
// randnum: next random number
/////////////////////////////////////////////////////////////////////////////
int next_random_int(rng_int_type* rng_int){
return (int) ((unsigned long int)next_random_lint(rng_int)>>32);
}
/////////////////////////////////////////////////////////////////////////////
// Description:
// Generates the next int random number
// Variables:
// rng_uniform: Random number generator state
// Return value:
// randnum: next random number
/////////////////////////////////////////////////////////////////////////////
double next_random_uniform(rng_uniform_type* rng_uniform){
unsigned long int irng = next_random_lint(&rng_uniform->rng_int);
double randnum = INV2POW53MIN1 * (double)(irng>>11);
randnum *= rng_uniform->upper - rng_uniform->lower;
randnum += rng_uniform->lower;
return randnum;
}
/////////////////////////////////////////////////////////////////////////////
// Description:
// Generates the next int random number
// Variables:
// rng_gaussian: Random number generator state
// Return value:
// randnum: next random number
/////////////////////////////////////////////////////////////////////////////
double next_random_gaussian(rng_gaussian_type* rng_gaussian){
if (rng_gaussian->generated) {
rng_gaussian->generated = false;
return rng_gaussian->z2 * rng_gaussian->stddev + rng_gaussian->mean;
}
double u1=next_random_uniform(&rng_gaussian->rng_uniform);
double u2=next_random_uniform(&rng_gaussian->rng_uniform);
while (u1 <= __DBL_EPSILON__){
u1 = next_random_uniform(&rng_gaussian->rng_uniform);
u2 = next_random_uniform(&rng_gaussian->rng_uniform);
}
double tmp1 = sqrt(-2.0*log(u1));
double tmp2 = twopi*u2;
rng_gaussian->z1 = tmp1*cos(tmp2);
rng_gaussian->z2 = tmp1*sin(tmp2);
rng_gaussian->generated = true;
return rng_gaussian->z1 * rng_gaussian->stddev + rng_gaussian->mean;
}
/////////////////////////////////////////////////////////////////////////////
// Description:
// Save rng state to a string
// Variables:
// rng_int: Random number generator state
// Return value:
// intstate: that contains all the state information
/////////////////////////////////////////////////////////////////////////////
char* get_random_state_int(rng_int_type* rng_int){
char* intstate = (char*)malloc(INT_STATELENGTH*sizeof(char));
char* tmpchar = intstate;
sprintf(tmpchar, "I %8x %8x", rng_int->seed, rng_int->mtidx);
tmpchar += 19;
for (int i=0; i<MTSTATESIZE; i++){
sprintf(tmpchar, "%17lx", rng_int->mtstate[i]);
tmpchar += 17;
}
return intstate;
}
/////////////////////////////////////////////////////////////////////////////
// Description:
// Save rng state to a string
// Variables:
// rng_uniform: Random number generator state
// Return value:
// uniformstate: that contains all the state information
/////////////////////////////////////////////////////////////////////////////
char* get_random_state_uniform(rng_uniform_type* rng_uniform){
char* unistate = (char*)malloc(UNIFORM_STATELENGTH*sizeof(char));
char* tmpchar = unistate;
void* v;
v = &rng_uniform->lower;
sprintf(tmpchar, "U%17lx", *((unsigned long int*)v));
tmpchar += 18;
v = &rng_uniform->upper;
sprintf(tmpchar, "%17lx ", *((unsigned long int*)v));
tmpchar += 17;
char* intstate = get_random_state_int(&rng_uniform->rng_int);
tmpchar++;
strcpy(tmpchar, intstate);
free(intstate);
intstate = NULL;
return unistate;
}
/////////////////////////////////////////////////////////////////////////////
// Description:
// Save rng state to a string
// Variables:
// rng_gaussian: Random number generator state
// Return value:
// gaussianstate: that contains all the state information
/////////////////////////////////////////////////////////////////////////////
char* get_random_state_gaussian(rng_gaussian_type* rng_gaussian){
char* gaussstate = (char*)malloc(GAUSS_STATELENGTH*sizeof(char));
char* tmpchar = gaussstate;
void* v;
v = &rng_gaussian->mean;
sprintf(tmpchar, "G%17lx", *((unsigned long int*)v));
tmpchar += 18;
v = &rng_gaussian->stddev;
sprintf(tmpchar, "%17lx", *((unsigned long int*)v));
tmpchar += 17;
v = &rng_gaussian->z1;
sprintf(tmpchar, "%17lx", *((unsigned long int*)v));
tmpchar += 17;
v = &rng_gaussian->z2;
sprintf(tmpchar, "%17lx", *((unsigned long int*)v));
tmpchar += 17;
v = &rng_gaussian->generated;
sprintf(tmpchar, "%9x ", *((unsigned int*)v));
tmpchar += 10;
char* unistate = get_random_state_uniform(&rng_gaussian->rng_uniform);
strcpy(tmpchar, unistate);
free(unistate);
unistate = NULL;
return gaussstate;
}
/////////////////////////////////////////////////////////////////////////////
// Description:
// Restore rng state from a string
// Variables:
// intstate: that contains all the state information
// Return value:
// rng_int: Random number generator state
/////////////////////////////////////////////////////////////////////////////
rng_int_type restore_rng_state_int(char* intstate){
rng_int_type rng_int;
char rngt;
sscanf(intstate, "%c", &rngt);
if (rngt != 'I'){
fprintf(stderr, "Error in restoring int random number generator.");
abort();
}
intstate += 2;
sscanf(intstate, " %8x %8x", (unsigned int*) &rng_int.seed, (unsigned int*) &rng_int.mtidx);
intstate += 18;
for (int i = 0; i<MTSTATESIZE; i++){
sscanf(intstate, "%17lx", (unsigned long*) &rng_int.mtstate[i]);
intstate += 17;
}
return rng_int;
}
/////////////////////////////////////////////////////////////////////////////
// Description:
// Restore rng state from a string
// Variables:
// uniformstate: that contains all the state information
// Return value:
// rng_uniform: Random number generator state
/////////////////////////////////////////////////////////////////////////////
rng_uniform_type restore_rng_state_uniform(char* uniformstate){
rng_uniform_type rng_uniform;
char rngt;
sscanf(uniformstate, "%c", &rngt);
if (rngt != 'U'){
fprintf(stderr, "Error in restoring uniform random number generator.");
abort();
}
void* v = NULL;
uniformstate += 1;
v = &rng_uniform.lower;
sscanf(uniformstate, "%17lx", (unsigned long int*)v);
uniformstate += 17;
v = &rng_uniform.upper;
sscanf(uniformstate, "%17lx", (unsigned long int*)v);
uniformstate += 17;
uniformstate++;
rng_uniform.rng_int = restore_rng_state_int(uniformstate);
return rng_uniform;
}
/////////////////////////////////////////////////////////////////////////////
// Description:
// Restore rng state from a string
// Variables:
// gaussianstate: that contains all the state information
// Return value:
// rng_gaussian: Random number generator state
/////////////////////////////////////////////////////////////////////////////
rng_gaussian_type restore_rng_state_gaussian(char* gaussianstate){
rng_gaussian_type rng_gaussian;
char rngt;
sscanf(gaussianstate, "%c", &rngt);
if (rngt != 'G'){
fprintf(stderr, "Error in restoring gaussian random number generator.");
abort();
}
void* v = NULL;
gaussianstate += 1;
v = &rng_gaussian.mean;
sscanf(gaussianstate, "%17lx", (unsigned long int*)v);
gaussianstate += 17;
v = &rng_gaussian.stddev;
sscanf(gaussianstate, "%17lx", (unsigned long int*)v);
gaussianstate += 17;
v = &rng_gaussian.z1;
sscanf(gaussianstate, "%17lx", (unsigned long int*)v);
gaussianstate += 17;
v = &rng_gaussian.z2;
sscanf(gaussianstate, "%17lx", (unsigned long int*)v);
gaussianstate += 17;
v = &rng_gaussian.generated;
sscanf(gaussianstate, "%9lx", (unsigned long int*)v);
gaussianstate += 9;
gaussianstate++;
rng_gaussian.rng_uniform = restore_rng_state_uniform(gaussianstate);
return rng_gaussian;
}