1 /*
2 * random.c
3 * Mersenne Twister random number generator
4 * Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>
5 * 18 Nov 2010 19:10:52
6 * slightly modified from mt19937ar.c available at
7 * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html
8 */
9
10 /*
11 A C-program for MT19937, with initialization improved 2002/1/26.
12 Coded by Takuji Nishimura and Makoto Matsumoto.
13
14 Before using, initialize the state by using init_genrand(seed)
15 or init_by_array(init_key, key_length).
16
17 Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
18 All rights reserved.
19
20 Redistribution and use in source and binary forms, with or without
21 modification, are permitted provided that the following conditions
22 are met:
23
24 1. Redistributions of source code must retain the above copyright
25 notice, this list of conditions and the following disclaimer.
26
27 2. Redistributions in binary form must reproduce the above copyright
28 notice, this list of conditions and the following disclaimer in the
29 documentation and/or other materials provided with the distribution.
30
31 3. The names of its contributors may not be used to endorse or promote
32 products derived from this software without specific prior written
33 permission.
34
35 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
39 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
41 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
42 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
43 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
44 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46
47
48 Any feedback is very welcome.
49 http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
50 email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
51 */
52
53 /* Period parameters */
54 #define N 624
55 #define M 397
56 #define MATRIX_A 0x9908b0dfUL /* constant vector a */
57 #define UPPER_MASK 0x80000000UL /* most significant w-r bits */
58 #define LOWER_MASK 0x7fffffffUL /* least significant r bits */
59
60 typedef struct {
61 unsigned long v[N]; /* the array for the state vector */
62 int i;
63 } MT;
64
65 #define mt (o->v)
66 #define mti (o->i)
67
68 /* initializes mt[N] with a seed */
init_genrand(MT * o,unsigned long s)69 static void init_genrand(MT *o, unsigned long s)
70 {
71 mt[0]= s & 0xffffffffUL;
72 for (mti=1; mti<N; mti++) {
73 mt[mti] =
74 (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
75 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
76 /* In the previous versions, MSBs of the seed affect */
77 /* only MSBs of the array mt[]. */
78 /* 2002/01/09 modified by Makoto Matsumoto */
79 mt[mti] &= 0xffffffffUL;
80 /* for >32 bit machines */
81 }
82 }
83
84 /* generates a random number on [0,0xffffffff]-interval */
genrand_int32(MT * o)85 static unsigned long genrand_int32(MT *o)
86 {
87 unsigned long y;
88 static unsigned long mag01[2]={0x0UL, MATRIX_A};
89 /* mag01[x] = x * MATRIX_A for x=0,1 */
90
91 if (mti >= N) { /* generate N words at one time */
92 int kk;
93
94 if (mti == N+1) /* if init_genrand() has not been called, */
95 init_genrand(o,5489UL); /* a default initial seed is used */
96
97 for (kk=0;kk<N-M;kk++) {
98 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
99 mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
100 }
101 for (;kk<N-1;kk++) {
102 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
103 mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
104 }
105 y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
106 mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
107
108 mti = 0;
109 }
110
111 y = mt[mti++];
112
113 /* Tempering */
114 y ^= (y >> 11);
115 y ^= (y << 7) & 0x9d2c5680UL;
116 y ^= (y << 15) & 0xefc60000UL;
117 y ^= (y >> 18);
118
119 return y;
120 }
121
122 /* These real versions are due to Isaku Wada, 2002/01/09 added */
123
124 #ifdef GENRAND32
125 /* generates a random number on [0,1)-real-interval */
genrand_real2(MT * o)126 static double genrand_real2(MT *o)
127 {
128 return genrand_int32(o)*(1.0/4294967296.0);
129 /* divided by 2^32 */
130 }
131 #define genrand genrand_real2
132 #else
133 /* generates a random number on [0,1) with 53-bit resolution*/
genrand_res53(MT * o)134 static double genrand_res53(MT *o)
135 {
136 unsigned long a=genrand_int32(o)>>5, b=genrand_int32(o)>>6;
137 return(a*67108864.0+b)*(1.0/9007199254740992.0);
138 }
139 #define genrand genrand_res53
140 #endif
141
142 #define AUTHOR "Mersenne Twister"
143