1 /* mt.c - A C program for MT199937, with initialization improved 2002-02-10.
4 2006-01-14 - Switched include from stdint.h to inttypes.h,
5 fixed a warning from gcc -Wall. --Josh
7 2003-07-01 - Stripped out stuff I didn't need, converted to
8 stdint.h types, changed source formatting a bit. --Josh
11 A C-program for MT19937, with initialization improved 2002-02-10.
12 Coded by Takuji Nishimura and Makoto Matsumoto.
13 This is a faster version by taking Shawn Cokus's optimization,
14 Matthe Bellew's simplification, Isaku Wada's real version.
16 Before using, initialize the state by using init_genrand(seed)
17 or init_by_array(init_key, key_length).
19 Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
22 Redistribution and use in source and binary forms, with or without
23 modification, are permitted provided that the following conditions
26 1. Redistributions of source code must retain the above copyright
27 notice, this list of conditions and the following disclaimer.
29 2. Redistributions in binary form must reproduce the above copyright
30 notice, this list of conditions and the following disclaimer in the
31 documentation and/or other materials provided with the distribution.
33 3. The names of its contributors may not be used to endorse or promote
34 products derived from this software without specific prior written
37 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
41 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
45 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
46 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
47 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 Any feedback is very welcome.
51 http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
52 email: m-mat@math.sci.hiroshima-u.ac.jp
57 /* Period parameters */
60 #define MATRIX_A 0x9908b0dfUL /* constant vector a */
61 #define UMASK 0x80000000UL /* most significant w-r bits */
62 #define LMASK 0x7fffffffUL /* least significant r bits */
63 #define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
64 #define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1UL ? MATRIX_A : 0UL))
66 static uint32_t state[N]; /* The array for the state vector */
69 static uint32_t *next;
71 /* Initializes state[N] with a seed. */
76 state[0]= s & 0xffffffffUL;
78 state[j] = (1812433253UL * (state[j-1] ^ (state[j-1] >> 30)) + j);
79 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
80 /* In the previous versions, MSBs of the seed affect */
81 /* only MSBs of the array state[]. */
82 /* 2002/01/09 modified by Makoto Matsumoto */
83 state[j] &= 0xffffffffUL; /* for >32 bit machines */
94 /* If mt_init() has not been called,
95 a default initial seed is used. */
96 if (initf==0) init_mt(5489UL);
101 for (j=N-M+1; --j; p++)
102 *p = p[M] ^ TWIST(p[0], p[1]);
105 *p = p[M-N] ^ TWIST(p[0], p[1]);
107 *p = p[M-N] ^ TWIST(p[0], state[0]);
110 /* Generates a random number in [0,0xffffffff] */
116 if (--left == 0) next_state();
121 y ^= (y << 7) & 0x9d2c5680UL;
122 y ^= (y << 15) & 0xefc60000UL;
131 return urnd()/(1.0 + UINT32_MAX);
137 return (urnd()-INT32_MIN)/(1.0 + UINT32_MAX);