3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): none yet.
26 * ***** END GPL LICENSE BLOCK *****
29 /* A C-program for MT19937: Real number version */
31 /* genrand() generates one pseudorandom real number (double) */
32 /* which is uniformly distributed on [0,1]-interval, for each */
33 /* call. sgenrand(seed) set initial values to the working area */
34 /* of 624 words. Before genrand(), sgenrand(seed) must be */
35 /* called once. (seed is any 32-bit integer except for 0). */
36 /* Integer generator is obtained by modifying two lines. */
37 /* Coded by Takuji Nishimura, considering the suggestions by */
38 /* Topher Cooper and Marc Rieffel in July-Aug. 1997. */
40 /* This library is free software; you can redistribute it and/or */
41 /* modify it under the terms of the GNU Library General Public */
42 /* License as published by the Free Software Foundation; either */
43 /* version 2 of the License, or (at your option) any later */
45 /* This library is distributed in the hope that it will be useful, */
46 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
47 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */
48 /* See the GNU Library General Public License for more details. */
49 /* You should have received a copy of the GNU Library General */
50 /* Public License along with this library; if not, write to the */
51 /* Free Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA */
54 /* Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura. */
55 /* When you use this, send an email to: matumoto@math.keio.ac.jp */
56 /* with an appropriate reference to your work. */
58 #include "MT_random.h"
60 /* Period parameters */
63 #define MATRIX_A 0x9908b0df /* constant vector a */
64 #define UPPER_MASK 0x80000000 /* most significant w-r bits */
65 #define LOWER_MASK 0x7fffffff /* least significant r bits */
67 /* Tempering parameters */
68 #define TEMPERING_MASK_B 0x9d2c5680
69 #define TEMPERING_MASK_C 0xefc60000
70 #define TEMPERING_SHIFT_U(y) (y >> 11)
71 #define TEMPERING_SHIFT_S(y) (y << 7)
72 #define TEMPERING_SHIFT_T(y) (y << 15)
73 #define TEMPERING_SHIFT_L(y) (y >> 18)
75 static unsigned int mt[N]; /* the array for the state vector */
76 static int mti = N+1; /* mti==N+1 means mt[N] is not initialized */
78 /* initializing the array with a NONZERO seed */
79 void MT_srand(unsigned int seed)
81 /* setting initial seeds to mt[N] using */
82 /* the generator Line 25 of Table 1 in */
83 /* [KNUTH 1981, The Art of Computer Programming */
84 /* Vol. 2 (2nd Ed.), pp102] */
85 mt[0] = seed & 0xffffffff;
86 for (mti = 1; mti < N; mti++)
87 mt[mti] = (69069 * mt[mti-1]) & 0xffffffff;
90 unsigned int MT_rand()
92 static unsigned int mag01[2] = { 0x0, MATRIX_A };
93 /* mag01[x] = x * MATRIX_A for x=0,1 */
97 if (mti >= N) { /* generate N words at one time */
100 if (mti == N+1) /* if sgenrand() has not been called, */
101 MT_srand(4357); /* a default initial seed is used */
103 for (kk = 0; kk < N - M; kk++) {
104 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);
105 mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
107 for (; kk < N-1; kk++) {
108 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);
109 mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
111 y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
112 mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
118 y ^= TEMPERING_SHIFT_U(y);
119 y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
120 y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
121 y ^= TEMPERING_SHIFT_L(y);
132 /* Tempering parameters */
133 #undef TEMPERING_MASK_B
134 #undef TEMPERING_MASK_C
135 #undef TEMPERING_SHIFT_U
136 #undef TEMPERING_SHIFT_S
137 #undef TEMPERING_SHIFT_T
138 #undef TEMPERING_SHIFT_L