2 ** mt19937ar.c - MT Random functions
4 ** Copyright (C) 1997 - 2016, Makoto Matsumoto and Takuji Nishimura,
5 ** All rights reserved.
7 ** Permission is hereby granted, free of charge, to any person obtaining
8 ** a copy of this software and associated documentation files (the
9 ** "Software"), to deal in the Software without restriction, including
10 ** without limitation the rights to use, copy, modify, merge, publish,
11 ** distribute, sublicense, and/or sell copies of the Software, and to
12 ** permit persons to whom the Software is furnished to do so, subject to
13 ** the following conditions:
15 ** The above copyright notice and this permission notice shall be
16 ** included in all copies or substantial portions of the Software.
18 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 ** [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
28 ** Any feedback is very welcome.
29 ** http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
30 ** email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
32 ** This version is modified by mruby developers. If you see any problem,
33 ** contact us first at https://github.com/mruby/mruby/issues
37 #include "mt19937ar.h"
39 /* Period parameters */
42 #define MATRIX_A 0x9908b0dfUL /* constant vector a */
43 #define UPPER_MASK 0x80000000UL /* most significant w-r bits */
44 #define LOWER_MASK 0x7fffffffUL /* least significant r bits */
47 static unsigned long mt[N]; /* the array for the state vector */
48 static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */
49 #endif /* dead_code */
51 void mrb_random_init_genrand(mt_state *t, unsigned long s)
53 t->mt[0]= s & 0xffffffffUL;
54 for (t->mti=1; t->mti<N; t->mti++) {
55 t->mt[t->mti] = (1812433253UL * (t->mt[t->mti-1] ^ (t->mt[t->mti-1] >> 30)) + t->mti);
56 t->mt[t->mti] &= 0xffffffffUL;
60 unsigned long mrb_random_genrand_int32(mt_state *t)
63 static const unsigned long mag01[2]={0x0UL, MATRIX_A};
64 /* mag01[x] = x * MATRIX_A for x=0,1 */
66 if (t->mti >= N) { /* generate N words at one time */
69 if (t->mti == N+1) /* if init_genrand() has not been called, */
70 mrb_random_init_genrand(t, 5489UL); /* a default initial seed is used */
72 for (kk=0;kk<N-M;kk++) {
73 y = (t->mt[kk]&UPPER_MASK)|(t->mt[kk+1]&LOWER_MASK);
74 t->mt[kk] = t->mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
77 y = (t->mt[kk]&UPPER_MASK)|(t->mt[kk+1]&LOWER_MASK);
78 t->mt[kk] = t->mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
80 y = (t->mt[N-1]&UPPER_MASK)|(t->mt[0]&LOWER_MASK);
81 t->mt[N-1] = t->mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
90 y ^= (y << 7) & 0x9d2c5680UL;
91 y ^= (y << 15) & 0xefc60000UL;
99 double mrb_random_genrand_real1(mt_state *t)
101 mrb_random_genrand_int32(t);
102 t->gen.double_ = t->gen.int_*(1.0/4294967295.0);
103 return t->gen.double_;
104 /* divided by 2^32-1 */
107 #if 0 /* dead_code */
108 /* initializes mt[N] with a seed */
109 void init_genrand(unsigned long s)
111 mt[0]= s & 0xffffffffUL;
112 for (mti=1; mti<N; mti++) {
113 mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
114 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
115 /* In the previous versions, MSBs of the seed affect */
116 /* only MSBs of the array mt[]. */
117 /* 2002/01/09 modified by Makoto Matsumoto */
118 mt[mti] &= 0xffffffffUL;
119 /* for >32 bit machines */
123 /* initialize by an array with array-length */
124 /* init_key is the array for initializing keys */
125 /* key_length is its length */
126 /* slight change for C++, 2004/2/26 */
127 void init_by_array(unsigned long init_key[], int key_length)
130 init_genrand(19650218UL);
132 k = (N>key_length ? N : key_length);
134 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
135 + init_key[j] + j; /* non linear */
136 mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
138 if (i>=N) { mt[0] = mt[N-1]; i=1; }
139 if (j>=key_length) j=0;
141 for (k=N-1; k; k--) {
142 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
143 - i; /* non linear */
144 mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
146 if (i>=N) { mt[0] = mt[N-1]; i=1; }
149 mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
152 /* generates a random number on [0,0xffffffff]-interval */
153 unsigned long genrand_int32(void)
156 static const unsigned long mag01[2]={0x0UL, MATRIX_A};
157 /* mag01[x] = x * MATRIX_A for x=0,1 */
159 if (mti >= N) { /* generate N words at one time */
162 if (mti == N+1) /* if init_genrand() has not been called, */
163 init_genrand(5489UL); /* a default initial seed is used */
165 for (kk=0;kk<N-M;kk++) {
166 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
167 mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
170 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
171 mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
173 y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
174 mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
183 y ^= (y << 7) & 0x9d2c5680UL;
184 y ^= (y << 15) & 0xefc60000UL;
190 /* generates a random number on [0,0x7fffffff]-interval */
191 long genrand_int31(void)
193 return (long)(genrand_int32()>>1);
196 /* generates a random number on [0,1]-real-interval */
197 double genrand_real1(void)
199 return genrand_int32()*(1.0/4294967295.0);
200 /* divided by 2^32-1 */
203 /* generates a random number on [0,1)-real-interval */
204 double genrand_real2(void)
206 return genrand_int32()*(1.0/4294967296.0);
207 /* divided by 2^32 */
210 /* generates a random number on (0,1)-real-interval */
211 double genrand_real3(void)
213 return (((double)genrand_int32()) + 0.5)*(1.0/4294967296.0);
214 /* divided by 2^32 */
217 /* generates a random number on [0,1) with 53-bit resolution*/
218 double genrand_res53(void)
220 unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6;
221 return(a*67108864.0+b)*(1.0/9007199254740992.0);
223 /* These real versions are due to Isaku Wada, 2002/01/09 added */
224 #endif /* dead_code */