JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
oops, have to check all 9 buckets.
[vor.git] / mt.c
1 /* mt.c - A C program for MT199937, with initialization improved 2002-02-10.
2
3    -----
4    2003-07-01 - Stripped out stuff I didn't need, converted to
5    stdint.h types, changed source formatting a bit.  --Josh
6    -----
7
8    A C-program for MT19937, with initialization improved 2002-02-10.
9    Coded by Takuji Nishimura and Makoto Matsumoto.
10    This is a faster version by taking Shawn Cokus's optimization,
11    Matthe Bellew's simplification, Isaku Wada's real version.
12
13    Before using, initialize the state by using init_genrand(seed) 
14    or init_by_array(init_key, key_length).
15
16    Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
17    All rights reserved.                          
18
19    Redistribution and use in source and binary forms, with or without
20    modification, are permitted provided that the following conditions
21    are met:
22
23      1. Redistributions of source code must retain the above copyright
24         notice, this list of conditions and the following disclaimer.
25
26      2. Redistributions in binary form must reproduce the above copyright
27         notice, this list of conditions and the following disclaimer in the
28         documentation and/or other materials provided with the distribution.
29
30      3. The names of its contributors may not be used to endorse or promote 
31         products derived from this software without specific prior written 
32         permission.
33
34    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
38    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
40    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
41    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
42    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
43    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45
46
47    Any feedback is very welcome.
48    http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
49    email: m-mat@math.sci.hiroshima-u.ac.jp
50 */
51
52 #include "mt.h"
53
54 /* Period parameters */  
55 #define N 624
56 #define M 397
57 #define MATRIX_A 0x9908b0dfUL   /* constant vector a */
58 #define UMASK 0x80000000UL /* most significant w-r bits */
59 #define LMASK 0x7fffffffUL /* least significant r bits */
60 #define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
61 #define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1UL ? MATRIX_A : 0UL))
62
63 static uint32_t state[N]; /* The array for the state vector  */
64 static int left = 1;
65 static int initf = 0;
66 static uint32_t *next;
67
68 /* Initializes state[N] with a seed. */
69 void
70 init_mt(uint32_t s)
71 {
72     int j;
73     state[0]= s & 0xffffffffUL;
74     for (j=1; j<N; j++) {
75         state[j] = (1812433253UL * (state[j-1] ^ (state[j-1] >> 30)) + j); 
76         /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
77         /* In the previous versions, MSBs of the seed affect   */
78         /* only MSBs of the array state[].                        */
79         /* 2002/01/09 modified by Makoto Matsumoto             */
80         state[j] &= 0xffffffffUL;  /* for >32 bit machines */
81     }
82     left = 1; initf = 1;
83 }
84
85 static void
86 next_state(void)
87 {
88     uint32_t *p=state;
89     int j;
90
91     /* If mt_init() has not been called,
92            a default initial seed is used. */
93     if (initf==0) init_mt(5489UL);
94
95     left = N;
96     next = state;
97     
98     for (j=N-M+1; --j; p++) 
99         *p = p[M] ^ TWIST(p[0], p[1]);
100
101     for (j=M; --j; p++) 
102         *p = p[M-N] ^ TWIST(p[0], p[1]);
103
104     *p = p[M-N] ^ TWIST(p[0], state[0]);
105 }
106
107 /* Generates a random number in [0,0xffffffff] */
108 uint32_t
109 urnd(void)
110 {
111     uint32_t y;
112
113     if (--left == 0) next_state();
114     y = *next++;
115
116     /* Tempering */
117     y ^= (y >> 11);
118     y ^= (y << 7) & 0x9d2c5680UL;
119     y ^= (y << 15) & 0xefc60000UL;
120     y ^= (y >> 18);
121
122     return y;
123 }
124
125 float
126 frnd(void)
127 {
128         return urnd()/(float)0x100000000UL;
129 }
130
131 float
132 crnd(void)
133 {
134         return (urnd()-0x80000000)/(float)0x100000000UL;
135 }