-
Notifications
You must be signed in to change notification settings - Fork 2
/
mt19937ar-cok.c
268 lines (220 loc) · 7.51 KB
/
mt19937ar-cok.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
/*
* Differences from official version available at
* http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html
* - Created header file mt19937ar-cok.h
* - Changed names to mt_* namespace
* - Fixed coding style to be consistent with project in general
* - Changed long to int32_t (which seems to be what was really intended)
*/
/*
A C-program for MT19937, with initialization improved 2002/2/10.
Coded by Takuji Nishimura and Makoto Matsumoto.
This is a faster version by taking Shawn Cokus's optimization,
Matthe Bellew's simplification, Isaku Wada's real version.
Before using, initialize the mt_state by using mt_init_genrand(seed)
or mt_init_by_array(init_key, key_length).
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The names of its contributors may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Any feedback is very welcome.
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
*/
#include <stdio.h>
#include <stdint.h>
#include "mt19937ar-cok.h"
/* Period parameters */
#define MT_N 624
#define MT_M 397
#define MT_MATRIX_A 0x9908b0dfUL /* constant vector a */
#define MT_UMASK 0x80000000UL /* most significant w-r bits */
#define MT_LMASK 0x7fffffffUL /* least significant r bits */
#define MT_MIXBITS(u,v) ( ((u) & MT_UMASK) | ((v) & MT_LMASK) )
#define MT_TWIST(u,v) ((MT_MIXBITS(u,v) >> 1) ^ ((v)&1UL ? MT_MATRIX_A : 0UL))
static uint32_t mt_state[MT_N]; /* the array for the mt_state vector */
static int mt_left = 1;
static int mt_initf = 0;
static uint32_t *mt_next;
/* initializes mt_state[MT_N] with a seed */
void mt_init_genrand(uint32_t s)
{
int j;
mt_state[0] = s & 0xffffffffUL;
for (j = 1; j < MT_N; j++) {
mt_state[j] = (1812433253UL * (mt_state[j - 1] ^ (mt_state[j - 1] >> 30)) + j);
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
/* In the previous versions, MSBs of the seed affect */
/* only MSBs of the array mt_state[]. */
/* 2002/01/09 modified by Makoto Matsumoto */
mt_state[j] &= 0xffffffffUL; /* for >32 bit machines */
}
mt_left = 1; mt_initf = 1;
}
/* initialize by an array with array-length */
/* init_key is the array for initializing keys */
/* key_length is its length */
/* slight change for C++, 2004/2/26 */
void mt_init_by_array(uint32_t init_key[], int key_length)
{
int i, j, k;
mt_init_genrand(19650218UL);
i = 1; j = 0;
k = (MT_N > key_length ? MT_N : key_length);
for (; k; k--) {
mt_state[i] = (mt_state[i] ^ ((mt_state[i - 1] ^ (mt_state[i - 1] >> 30)) * 1664525UL))
+ init_key[j] + j; /* non linear */
mt_state[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
i++; j++;
if (i>=MT_N) { mt_state[0] = mt_state[MT_N - 1]; i = 1; }
if (j>=key_length) j=0;
}
for (k = MT_N - 1; k; k--) {
mt_state[i] = (mt_state[i] ^ ((mt_state[i - 1] ^ (mt_state[i - 1] >> 30)) * 1566083941UL))
- i; /* non linear */
mt_state[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
i++;
if (i >= MT_N) { mt_state[0] = mt_state[MT_N - 1]; i = 1; }
}
mt_state[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
mt_left = 1; mt_initf = 1;
}
static void mt_next_state(void)
{
uint32_t *p=mt_state;
int j;
/* if mt_init_genrand() has not been called, */
/* a default initial seed is used */
if (mt_initf == 0) mt_init_genrand(5489UL);
mt_left = MT_N;
mt_next = mt_state;
for (j = MT_N - MT_M + 1; --j; p++)
*p = p[MT_M] ^ MT_TWIST(p[0], p[1]);
for (j = MT_M; --j; p++)
*p = p[MT_M - MT_N] ^ MT_TWIST(p[0], p[1]);
*p = p[MT_M - MT_N] ^ MT_TWIST(p[0], mt_state[0]);
}
/* generates a random number on [0,0xffffffff]-interval */
uint32_t mt_genrand_int32(void)
{
uint32_t y;
if (--mt_left == 0)
mt_next_state();
y = *mt_next++;
/* Tempering */
y ^= (y >> 11);
y ^= (y << 7) & 0x9d2c5680UL;
y ^= (y << 15) & 0xefc60000UL;
y ^= (y >> 18);
return y;
}
/* generates a random number on [0,0x7fffffff]-interval */
int32_t mt_genrand_int31(void)
{
uint32_t y;
if (--mt_left == 0)
mt_next_state();
y = *mt_next++;
/* Tempering */
y ^= (y >> 11);
y ^= (y << 7) & 0x9d2c5680UL;
y ^= (y << 15) & 0xefc60000UL;
y ^= (y >> 18);
return (long)(y >> 1);
}
/* generates a random number on [0,1]-real-interval */
double mt_genrand_real1(void)
{
uint32_t y;
if (--mt_left == 0)
mt_next_state();
y = *mt_next++;
/* Tempering */
y ^= (y >> 11);
y ^= (y << 7) & 0x9d2c5680UL;
y ^= (y << 15) & 0xefc60000UL;
y ^= (y >> 18);
return (double)y * (1.0 / 4294967295.0);
/* divided by 2^32-1 */
}
/* generates a random number on [0,1)-real-interval */
double mt_genrand_real2(void)
{
uint32_t y;
if (--mt_left == 0)
mt_next_state();
y = *mt_next++;
/* Tempering */
y ^= (y >> 11);
y ^= (y << 7) & 0x9d2c5680UL;
y ^= (y << 15) & 0xefc60000UL;
y ^= (y >> 18);
return (double)y * (1.0 / 4294967296.0);
/* divided by 2^32 */
}
/* generates a random number on (0,1)-real-interval */
double mt_genrand_real3(void)
{
uint32_t y;
if (--mt_left == 0)
mt_next_state();
y = *mt_next++;
/* Tempering */
y ^= (y >> 11);
y ^= (y << 7) & 0x9d2c5680UL;
y ^= (y << 15) & 0xefc60000UL;
y ^= (y >> 18);
return ((double)y + 0.5) * (1.0 / 4294967296.0);
/* divided by 2^32 */
}
/* generates a random number on [0,1) with 53-bit resolution*/
double mt_genrand_res53(void)
{
uint32_t a = mt_genrand_int32() >> 5, b = mt_genrand_int32() >> 6;
return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0);
}
/* These real versions are due to Isaku Wada, 2002/01/09 added */
#if 0
int main(void)
{
int i;
uint32_t init[4] = {0x123, 0x234, 0x345, 0x456}, length = 4;
mt_init_by_array(init, length);
/* This is an example of initializing by an array.
You may use mt_init_genrand(seed) with any 32bit integer
as a seed for a simpler initialization */
printf("1000 outputs of mt_genrand_int32()\n");
for (i = 0; i < 1000; i++) {
printf("%10lu ", mt_genrand_int32());
if (i % 5 == 4)
printf("\n");
}
printf("\n1000 outputs of genrand_real2()\n");
for (i = 0; i < 1000; i++) {
printf("%10.8f ", mt_genrand_real2());
if (i % 5 == 4)
printf("\n");
}
return 0;
}
#endif