JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
compile in version number from configure.ac
[vor.git] / mt.c
1 /* mt.c - A C program for MT199937, with initialization improved 2002-02-10.
2
3    -----
4    2006-01-14 - Switched include from stdint.h to inttypes.h,
5    fixed a warning from gcc -Wall.  --Josh
6    -----
7    2003-07-01 - Stripped out stuff I didn't need, converted to
8    stdint.h types, changed source formatting a bit.  --Josh
9    -----
10
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.
15
16    Before using, initialize the state by using init_genrand(seed) 
17    or init_by_array(init_key, key_length).
18
19    Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
20    All rights reserved.                          
21
22    Redistribution and use in source and binary forms, with or without
23    modification, are permitted provided that the following conditions
24    are met:
25
26      1. Redistributions of source code must retain the above copyright
27         notice, this list of conditions and the following disclaimer.
28
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.
32
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 
35         permission.
36
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.
48
49
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
53 */
54
55 #include "mt.h"
56
57 /* Period parameters */  
58 #define N 624
59 #define M 397
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))
65
66 static uint32_t state[N]; /* The array for the state vector  */
67 static int left = 1;
68 static int initf = 0;
69 static uint32_t *next;
70
71 /* Initializes state[N] with a seed. */
72 void
73 init_mt(uint32_t s)
74 {
75     int j;
76     state[0]= s & 0xffffffffUL;
77     for (j=1; j<N; j++) {
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 */
84     }
85     left = 1; initf = 1;
86 }
87
88 static void
89 next_state(void)
90 {
91     uint32_t *p=state;
92     int j;
93
94     /* If mt_init() has not been called,
95            a default initial seed is used. */
96     if (initf==0) init_mt(5489UL);
97
98     left = N;
99     next = state;
100     
101     for (j=N-M+1; --j; p++) 
102         *p = p[M] ^ TWIST(p[0], p[1]);
103
104     for (j=M; --j; p++) 
105         *p = p[M-N] ^ TWIST(p[0], p[1]);
106
107     *p = p[M-N] ^ TWIST(p[0], state[0]);
108 }
109
110 /* Generates a random number in [0,0xffffffff] */
111 uint32_t
112 urnd(void)
113 {
114     uint32_t y;
115
116     if (--left == 0) next_state();
117     y = *next++;
118
119     /* Tempering */
120     y ^= (y >> 11);
121     y ^= (y << 7) & 0x9d2c5680UL;
122     y ^= (y << 15) & 0xefc60000UL;
123     y ^= (y >> 18);
124
125     return y;
126 }
127
128 float
129 frnd(void)
130 {
131         return urnd()/(1.0 + UINT32_MAX);
132 }
133
134 float
135 crnd(void)
136 {
137         return (urnd()-INT32_MIN)/(1.0 + UINT32_MAX);
138 }