[270] | 1 | /*
|
---|
| 2 | ** mt19937ar.c - MT Random functions
|
---|
| 3 | **
|
---|
[331] | 4 | ** Copyright (C) 1997 - 2016, Makoto Matsumoto and Takuji Nishimura,
|
---|
| 5 | ** All rights reserved.
|
---|
| 6 | **
|
---|
| 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:
|
---|
| 14 | **
|
---|
| 15 | ** The above copyright notice and this permission notice shall be
|
---|
| 16 | ** included in all copies or substantial portions of the Software.
|
---|
| 17 | **
|
---|
| 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.
|
---|
| 25 | **
|
---|
| 26 | ** [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
|
---|
| 27 | **
|
---|
| 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)
|
---|
| 31 | **
|
---|
| 32 | ** This version is modified by mruby developers. If you see any problem,
|
---|
| 33 | ** contact us first at https://github.com/mruby/mruby/issues
|
---|
[270] | 34 | */
|
---|
| 35 |
|
---|
[331] | 36 | #include <mruby.h>
|
---|
[270] | 37 | #include "mt19937ar.h"
|
---|
| 38 |
|
---|
| 39 | /* Period parameters */
|
---|
| 40 | /* #define N 624 */
|
---|
| 41 | #define M 397
|
---|
| 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 */
|
---|
| 45 |
|
---|
| 46 | #if 0 /* dead_code */
|
---|
| 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 */
|
---|
| 50 |
|
---|
| 51 | void mrb_random_init_genrand(mt_state *t, unsigned long s)
|
---|
| 52 | {
|
---|
| 53 | t->mt[0]= s & 0xffffffffUL;
|
---|
| 54 | for (t->mti=1; t->mti<N; t->mti++) {
|
---|
| 55 | t->mt[t->mti] =
|
---|
| 56 | (1812433253UL * (t->mt[t->mti-1] ^ (t->mt[t->mti-1] >> 30)) + t->mti);
|
---|
| 57 | t->mt[t->mti] &= 0xffffffffUL;
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | unsigned long mrb_random_genrand_int32(mt_state *t)
|
---|
| 62 | {
|
---|
| 63 | unsigned long y;
|
---|
| 64 | static const unsigned long mag01[2]={0x0UL, MATRIX_A};
|
---|
| 65 | /* mag01[x] = x * MATRIX_A for x=0,1 */
|
---|
| 66 |
|
---|
| 67 | if (t->mti >= N) { /* generate N words at one time */
|
---|
| 68 | int kk;
|
---|
| 69 |
|
---|
| 70 | if (t->mti == N+1) /* if init_genrand() has not been called, */
|
---|
| 71 | mrb_random_init_genrand(t, 5489UL); /* a default initial seed is used */
|
---|
| 72 |
|
---|
| 73 | for (kk=0;kk<N-M;kk++) {
|
---|
| 74 | y = (t->mt[kk]&UPPER_MASK)|(t->mt[kk+1]&LOWER_MASK);
|
---|
| 75 | t->mt[kk] = t->mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
|
---|
| 76 | }
|
---|
| 77 | for (;kk<N-1;kk++) {
|
---|
| 78 | y = (t->mt[kk]&UPPER_MASK)|(t->mt[kk+1]&LOWER_MASK);
|
---|
| 79 | t->mt[kk] = t->mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
|
---|
| 80 | }
|
---|
| 81 | y = (t->mt[N-1]&UPPER_MASK)|(t->mt[0]&LOWER_MASK);
|
---|
| 82 | t->mt[N-1] = t->mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
|
---|
| 83 |
|
---|
| 84 | t->mti = 0;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | y = t->mt[t->mti++];
|
---|
| 88 |
|
---|
| 89 | /* Tempering */
|
---|
| 90 | y ^= (y >> 11);
|
---|
| 91 | y ^= (y << 7) & 0x9d2c5680UL;
|
---|
| 92 | y ^= (y << 15) & 0xefc60000UL;
|
---|
| 93 | y ^= (y >> 18);
|
---|
| 94 |
|
---|
| 95 | t->gen.int_ = y;
|
---|
| 96 |
|
---|
| 97 | return y;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | double mrb_random_genrand_real1(mt_state *t)
|
---|
| 101 | {
|
---|
| 102 | mrb_random_genrand_int32(t);
|
---|
| 103 | t->gen.double_ = t->gen.int_*(1.0/4294967295.0);
|
---|
| 104 | return t->gen.double_;
|
---|
| 105 | /* divided by 2^32-1 */
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | #if 0 /* dead_code */
|
---|
| 109 | /* initializes mt[N] with a seed */
|
---|
| 110 | void init_genrand(unsigned long s)
|
---|
| 111 | {
|
---|
| 112 | mt[0]= s & 0xffffffffUL;
|
---|
| 113 | for (mti=1; mti<N; mti++) {
|
---|
| 114 | mt[mti] =
|
---|
| 115 | (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
|
---|
| 116 | /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
|
---|
| 117 | /* In the previous versions, MSBs of the seed affect */
|
---|
| 118 | /* only MSBs of the array mt[]. */
|
---|
| 119 | /* 2002/01/09 modified by Makoto Matsumoto */
|
---|
| 120 | mt[mti] &= 0xffffffffUL;
|
---|
| 121 | /* for >32 bit machines */
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | /* initialize by an array with array-length */
|
---|
| 126 | /* init_key is the array for initializing keys */
|
---|
| 127 | /* key_length is its length */
|
---|
| 128 | /* slight change for C++, 2004/2/26 */
|
---|
| 129 | void init_by_array(unsigned long init_key[], int key_length)
|
---|
| 130 | {
|
---|
| 131 | int i, j, k;
|
---|
| 132 | init_genrand(19650218UL);
|
---|
| 133 | i=1; j=0;
|
---|
| 134 | k = (N>key_length ? N : key_length);
|
---|
| 135 | for (; k; k--) {
|
---|
| 136 | mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
|
---|
| 137 | + init_key[j] + j; /* non linear */
|
---|
| 138 | mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
|
---|
| 139 | i++; j++;
|
---|
| 140 | if (i>=N) { mt[0] = mt[N-1]; i=1; }
|
---|
| 141 | if (j>=key_length) j=0;
|
---|
| 142 | }
|
---|
| 143 | for (k=N-1; k; k--) {
|
---|
| 144 | mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
|
---|
| 145 | - i; /* non linear */
|
---|
| 146 | mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
|
---|
| 147 | i++;
|
---|
| 148 | if (i>=N) { mt[0] = mt[N-1]; i=1; }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /* generates a random number on [0,0xffffffff]-interval */
|
---|
| 155 | unsigned long genrand_int32(void)
|
---|
| 156 | {
|
---|
| 157 | unsigned long y;
|
---|
| 158 | static const unsigned long mag01[2]={0x0UL, MATRIX_A};
|
---|
| 159 | /* mag01[x] = x * MATRIX_A for x=0,1 */
|
---|
| 160 |
|
---|
| 161 | if (mti >= N) { /* generate N words at one time */
|
---|
| 162 | int kk;
|
---|
| 163 |
|
---|
| 164 | if (mti == N+1) /* if init_genrand() has not been called, */
|
---|
| 165 | init_genrand(5489UL); /* a default initial seed is used */
|
---|
| 166 |
|
---|
| 167 | for (kk=0;kk<N-M;kk++) {
|
---|
| 168 | y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
|
---|
| 169 | mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
|
---|
| 170 | }
|
---|
| 171 | for (;kk<N-1;kk++) {
|
---|
| 172 | y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
|
---|
| 173 | mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
|
---|
| 174 | }
|
---|
| 175 | y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
|
---|
| 176 | mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
|
---|
| 177 |
|
---|
| 178 | mti = 0;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | y = mt[mti++];
|
---|
| 182 |
|
---|
| 183 | /* Tempering */
|
---|
| 184 | y ^= (y >> 11);
|
---|
| 185 | y ^= (y << 7) & 0x9d2c5680UL;
|
---|
| 186 | y ^= (y << 15) & 0xefc60000UL;
|
---|
| 187 | y ^= (y >> 18);
|
---|
| 188 |
|
---|
| 189 | return y;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | /* generates a random number on [0,0x7fffffff]-interval */
|
---|
| 193 | long genrand_int31(void)
|
---|
| 194 | {
|
---|
| 195 | return (long)(genrand_int32()>>1);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | /* generates a random number on [0,1]-real-interval */
|
---|
| 199 | double genrand_real1(void)
|
---|
| 200 | {
|
---|
| 201 | return genrand_int32()*(1.0/4294967295.0);
|
---|
| 202 | /* divided by 2^32-1 */
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | /* generates a random number on [0,1)-real-interval */
|
---|
| 206 | double genrand_real2(void)
|
---|
| 207 | {
|
---|
| 208 | return genrand_int32()*(1.0/4294967296.0);
|
---|
| 209 | /* divided by 2^32 */
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | /* generates a random number on (0,1)-real-interval */
|
---|
| 213 | double genrand_real3(void)
|
---|
| 214 | {
|
---|
| 215 | return (((double)genrand_int32()) + 0.5)*(1.0/4294967296.0);
|
---|
| 216 | /* divided by 2^32 */
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | /* generates a random number on [0,1) with 53-bit resolution*/
|
---|
| 220 | double genrand_res53(void)
|
---|
| 221 | {
|
---|
| 222 | unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6;
|
---|
| 223 | return(a*67108864.0+b)*(1.0/9007199254740992.0);
|
---|
| 224 | }
|
---|
| 225 | /* These real versions are due to Isaku Wada, 2002/01/09 added */
|
---|
| 226 | #endif /* dead_code */
|
---|