2 This is based on trimmed version of MT19937. To get the original version,
3 contact <http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html>.
5 The original copyright notice follows.
7 A C-program for MT19937, with initialization improved 2002/2/10.
8 Coded by Takuji Nishimura and Makoto Matsumoto.
9 This is a faster version by taking Shawn Cokus's optimization,
10 Matthe Bellew's simplification, Isaku Wada's real version.
12 Before using, initialize the state by using init_genrand(mt, seed)
13 or init_by_array(mt, init_key, key_length).
15 Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
18 Redistribution and use in source and binary forms, with or without
19 modification, are permitted provided that the following conditions
22 1. Redistributions of source code must retain the above copyright
23 notice, this list of conditions and the following disclaimer.
25 2. Redistributions in binary form must reproduce the above copyright
26 notice, this list of conditions and the following disclaimer in the
27 documentation and/or other materials provided with the distribution.
29 3. The names of its contributors may not be used to endorse or promote
30 products derived from this software without specific prior written
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
37 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
38 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
39 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
40 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 Any feedback is very welcome.
47 http://www.math.keio.ac.jp/matumoto/emt.html
48 email: matumoto@math.keio.ac.jp
51 /* Period parameters */
54 #define MATRIX_A 0x9908b0dfU /* constant vector a */
55 #define UMASK 0x80000000U /* most significant w-r bits */
56 #define LMASK 0x7fffffffU /* least significant r bits */
57 #define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
58 #define TWIST(u,v) ((MIXBITS((u),(v)) >> 1) ^ ((v)&1U ? MATRIX_A : 0U))
60 enum {MT_MAX_STATE
= N
};
63 /* assume int is enough to store 32bits */
64 uint32_t state
[N
]; /* the array for the state vector */
69 #define genrand_initialized(mt) ((mt)->next != 0)
70 #define uninit_genrand(mt) ((mt)->next = 0)
72 NO_SANITIZE("unsigned-integer-overflow", static void init_genrand(struct MT
*mt
, unsigned int s
));
73 NO_SANITIZE("unsigned-integer-overflow", static void init_by_array(struct MT
*mt
, const uint32_t init_key
[], int key_length
));
75 /* initializes state[N] with a seed */
77 init_genrand(struct MT
*mt
, unsigned int s
)
80 mt
->state
[0] = s
& 0xffffffffU
;
82 mt
->state
[j
] = (1812433253U * (mt
->state
[j
-1] ^ (mt
->state
[j
-1] >> 30)) + j
);
83 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
84 /* In the previous versions, MSBs of the seed affect */
85 /* only MSBs of the array state[]. */
86 /* 2002/01/09 modified by Makoto Matsumoto */
87 mt
->state
[j
] &= 0xffffffff; /* for >32 bit machines */
90 mt
->next
= mt
->state
+ N
;
93 /* initialize by an array with array-length */
94 /* init_key is the array for initializing keys */
95 /* key_length is its length */
96 /* slight change for C++, 2004/2/26 */
98 init_by_array(struct MT
*mt
, const uint32_t init_key
[], int key_length
)
101 init_genrand(mt
, 19650218U);
103 k
= (N
>key_length
? N
: key_length
);
105 mt
->state
[i
] = (mt
->state
[i
] ^ ((mt
->state
[i
-1] ^ (mt
->state
[i
-1] >> 30)) * 1664525U))
106 + init_key
[j
] + j
; /* non linear */
107 mt
->state
[i
] &= 0xffffffffU
; /* for WORDSIZE > 32 machines */
109 if (i
>=N
) { mt
->state
[0] = mt
->state
[N
-1]; i
=1; }
110 if (j
>=key_length
) j
=0;
112 for (k
=N
-1; k
; k
--) {
113 mt
->state
[i
] = (mt
->state
[i
] ^ ((mt
->state
[i
-1] ^ (mt
->state
[i
-1] >> 30)) * 1566083941U))
114 - i
; /* non linear */
115 mt
->state
[i
] &= 0xffffffffU
; /* for WORDSIZE > 32 machines */
117 if (i
>=N
) { mt
->state
[0] = mt
->state
[N
-1]; i
=1; }
120 mt
->state
[0] = 0x80000000U
; /* MSB is 1; assuring non-zero initial array */
124 next_state(struct MT
*mt
)
126 uint32_t *p
= mt
->state
;
130 mt
->next
= mt
->state
;
132 for (j
=N
-M
+1; --j
; p
++)
133 *p
= p
[M
] ^ TWIST(p
[0], p
[1]);
136 *p
= p
[M
-N
] ^ TWIST(p
[0], p
[1]);
138 *p
= p
[M
-N
] ^ TWIST(p
[0], mt
->state
[0]);
141 /* generates a random number on [0,0xffffffff]-interval */
143 genrand_int32(struct MT
*mt
)
145 /* mt must be initialized */
148 if (--mt
->left
<= 0) next_state(mt
);
153 y
^= (y
<< 7) & 0x9d2c5680;
154 y
^= (y
<< 15) & 0xefc60000;