|
65 | 65 | */ |
66 | 66 |
|
67 | 67 | /* eslint-disable */ |
68 | | -var MersenneTwister = function(seed) { |
| 68 | +var MersenneTwister = function (seed) { |
69 | 69 | if (seed == undefined) { |
70 | 70 | seed = new Date().getTime(); |
71 | | - } |
72 | | - /* Period parameters */ |
| 71 | + } |
| 72 | + /* Period parameters */ |
73 | 73 | this.N = 624; |
74 | 74 | this.M = 397; |
75 | | - this.MATRIX_A = 0x9908b0df; /* constant vector a */ |
| 75 | + this.MATRIX_A = 0x9908b0df; /* constant vector a */ |
76 | 76 | this.UPPER_MASK = 0x80000000; /* most significant w-r bits */ |
77 | 77 | this.LOWER_MASK = 0x7fffffff; /* least significant r bits */ |
78 | | - |
| 78 | + |
79 | 79 | this.mt = new Array(this.N); /* the array for the state vector */ |
80 | | - this.mti=this.N+1; /* mti==N+1 means mt[N] is not initialized */ |
| 80 | + this.mti = this.N + 1; /* mti==N+1 means mt[N] is not initialized */ |
81 | 81 |
|
82 | 82 | this.init_genrand(seed); |
83 | | -} |
84 | | - |
| 83 | +}; |
| 84 | + |
85 | 85 | /* initializes mt[N] with a seed */ |
86 | | -MersenneTwister.prototype.init_genrand = function(s) { |
| 86 | +MersenneTwister.prototype.init_genrand = function (s) { |
87 | 87 | this.mt[0] = s >>> 0; |
88 | | - for (this.mti=1; this.mti<this.N; this.mti++) { |
89 | | - var s = this.mt[this.mti-1] ^ (this.mt[this.mti-1] >>> 30); |
90 | | - this.mt[this.mti] = (((((s & 0xffff0000) >>> 16) * 1812433253) << 16) + (s & 0x0000ffff) * 1812433253) |
91 | | - + this.mti; |
92 | | - /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ |
93 | | - /* In the previous versions, MSBs of the seed affect */ |
94 | | - /* only MSBs of the array mt[]. */ |
95 | | - /* 2002/01/09 modified by Makoto Matsumoto */ |
96 | | - this.mt[this.mti] >>>= 0; |
97 | | - /* for >32 bit machines */ |
| 88 | + for (this.mti = 1; this.mti < this.N; this.mti++) { |
| 89 | + var s = this.mt[this.mti - 1] ^ (this.mt[this.mti - 1] >>> 30); |
| 90 | + this.mt[this.mti] = ((((s & 0xffff0000) >>> 16) * 1812433253) << 16) + (s & 0x0000ffff) * 1812433253 + this.mti; |
| 91 | + /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ |
| 92 | + /* In the previous versions, MSBs of the seed affect */ |
| 93 | + /* only MSBs of the array mt[]. */ |
| 94 | + /* 2002/01/09 modified by Makoto Matsumoto */ |
| 95 | + this.mt[this.mti] >>>= 0; |
| 96 | + /* for >32 bit machines */ |
98 | 97 | } |
99 | | -} |
100 | | - |
| 98 | +}; |
| 99 | + |
101 | 100 | /* initialize by an array with array-length */ |
102 | 101 | /* init_key is the array for initializing keys */ |
103 | 102 | /* key_length is its length */ |
104 | 103 | /* slight change for C++, 2004/2/26 */ |
105 | | -MersenneTwister.prototype.init_by_array = function(init_key, key_length) { |
| 104 | +MersenneTwister.prototype.init_by_array = function (init_key, key_length) { |
106 | 105 | var i, j, k; |
107 | 106 | this.init_genrand(19650218); |
108 | | - i=1; j=0; |
109 | | - k = (this.N>key_length ? this.N : key_length); |
| 107 | + i = 1; |
| 108 | + j = 0; |
| 109 | + k = this.N > key_length ? this.N : key_length; |
110 | 110 | for (; k; k--) { |
111 | | - var s = this.mt[i-1] ^ (this.mt[i-1] >>> 30) |
112 | | - this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1664525) << 16) + ((s & 0x0000ffff) * 1664525))) |
113 | | - + init_key[j] + j; /* non linear */ |
| 111 | + var s = this.mt[i - 1] ^ (this.mt[i - 1] >>> 30); |
| 112 | + this.mt[i] = |
| 113 | + (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1664525) << 16) + (s & 0x0000ffff) * 1664525)) + |
| 114 | + init_key[j] + |
| 115 | + j; /* non linear */ |
114 | 116 | this.mt[i] >>>= 0; /* for WORDSIZE > 32 machines */ |
115 | | - i++; j++; |
116 | | - if (i>=this.N) { this.mt[0] = this.mt[this.N-1]; i=1; } |
117 | | - if (j>=key_length) j=0; |
| 117 | + i++; |
| 118 | + j++; |
| 119 | + if (i >= this.N) { |
| 120 | + this.mt[0] = this.mt[this.N - 1]; |
| 121 | + i = 1; |
| 122 | + } |
| 123 | + if (j >= key_length) j = 0; |
118 | 124 | } |
119 | | - for (k=this.N-1; k; k--) { |
120 | | - var s = this.mt[i-1] ^ (this.mt[i-1] >>> 30); |
121 | | - this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1566083941) << 16) + (s & 0x0000ffff) * 1566083941)) |
122 | | - - i; /* non linear */ |
| 125 | + for (k = this.N - 1; k; k--) { |
| 126 | + var s = this.mt[i - 1] ^ (this.mt[i - 1] >>> 30); |
| 127 | + this.mt[i] = |
| 128 | + (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1566083941) << 16) + (s & 0x0000ffff) * 1566083941)) - |
| 129 | + i; /* non linear */ |
123 | 130 | this.mt[i] >>>= 0; /* for WORDSIZE > 32 machines */ |
124 | 131 | i++; |
125 | | - if (i>=this.N) { this.mt[0] = this.mt[this.N-1]; i=1; } |
| 132 | + if (i >= this.N) { |
| 133 | + this.mt[0] = this.mt[this.N - 1]; |
| 134 | + i = 1; |
| 135 | + } |
126 | 136 | } |
127 | 137 |
|
128 | | - this.mt[0] = 0x80000000; /* MSB is 1; assuring non-zero initial array */ |
129 | | -} |
130 | | - |
| 138 | + this.mt[0] = 0x80000000; /* MSB is 1; assuring non-zero initial array */ |
| 139 | +}; |
| 140 | + |
131 | 141 | /* generates a random number on [0,0xffffffff]-interval */ |
132 | | -MersenneTwister.prototype.genrand_int32 = function() { |
| 142 | +MersenneTwister.prototype.genrand_int32 = function () { |
133 | 143 | var y; |
134 | 144 | var mag01 = new Array(0x0, this.MATRIX_A); |
135 | 145 | /* mag01[x] = x * MATRIX_A for x=0,1 */ |
136 | 146 |
|
137 | | - if (this.mti >= this.N) { /* generate N words at one time */ |
| 147 | + if (this.mti >= this.N) { |
| 148 | + /* generate N words at one time */ |
138 | 149 | var kk; |
139 | 150 |
|
140 | | - if (this.mti == this.N+1) /* if init_genrand() has not been called, */ |
| 151 | + if (this.mti == this.N + 1) |
| 152 | + /* if init_genrand() has not been called, */ |
141 | 153 | this.init_genrand(5489); /* a default initial seed is used */ |
142 | 154 |
|
143 | | - for (kk=0;kk<this.N-this.M;kk++) { |
144 | | - y = (this.mt[kk]&this.UPPER_MASK)|(this.mt[kk+1]&this.LOWER_MASK); |
145 | | - this.mt[kk] = this.mt[kk+this.M] ^ (y >>> 1) ^ mag01[y & 0x1]; |
| 155 | + for (kk = 0; kk < this.N - this.M; kk++) { |
| 156 | + y = (this.mt[kk] & this.UPPER_MASK) | (this.mt[kk + 1] & this.LOWER_MASK); |
| 157 | + this.mt[kk] = this.mt[kk + this.M] ^ (y >>> 1) ^ mag01[y & 0x1]; |
146 | 158 | } |
147 | | - for (;kk<this.N-1;kk++) { |
148 | | - y = (this.mt[kk]&this.UPPER_MASK)|(this.mt[kk+1]&this.LOWER_MASK); |
149 | | - this.mt[kk] = this.mt[kk+(this.M-this.N)] ^ (y >>> 1) ^ mag01[y & 0x1]; |
| 159 | + for (; kk < this.N - 1; kk++) { |
| 160 | + y = (this.mt[kk] & this.UPPER_MASK) | (this.mt[kk + 1] & this.LOWER_MASK); |
| 161 | + this.mt[kk] = this.mt[kk + (this.M - this.N)] ^ (y >>> 1) ^ mag01[y & 0x1]; |
150 | 162 | } |
151 | | - y = (this.mt[this.N-1]&this.UPPER_MASK)|(this.mt[0]&this.LOWER_MASK); |
152 | | - this.mt[this.N-1] = this.mt[this.M-1] ^ (y >>> 1) ^ mag01[y & 0x1]; |
| 163 | + y = (this.mt[this.N - 1] & this.UPPER_MASK) | (this.mt[0] & this.LOWER_MASK); |
| 164 | + this.mt[this.N - 1] = this.mt[this.M - 1] ^ (y >>> 1) ^ mag01[y & 0x1]; |
153 | 165 |
|
154 | 166 | this.mti = 0; |
155 | 167 | } |
156 | 168 |
|
157 | 169 | y = this.mt[this.mti++]; |
158 | 170 |
|
159 | 171 | /* Tempering */ |
160 | | - y ^= (y >>> 11); |
| 172 | + y ^= y >>> 11; |
161 | 173 | y ^= (y << 7) & 0x9d2c5680; |
162 | 174 | y ^= (y << 15) & 0xefc60000; |
163 | | - y ^= (y >>> 18); |
| 175 | + y ^= y >>> 18; |
164 | 176 |
|
165 | 177 | return y >>> 0; |
166 | | -} |
167 | | - |
| 178 | +}; |
| 179 | + |
168 | 180 | /* generates a random number on [0,0x7fffffff]-interval */ |
169 | | -MersenneTwister.prototype.genrand_int31 = function() { |
170 | | - return (this.genrand_int32()>>>1); |
171 | | -} |
172 | | - |
| 181 | +MersenneTwister.prototype.genrand_int31 = function () { |
| 182 | + return this.genrand_int32() >>> 1; |
| 183 | +}; |
| 184 | + |
173 | 185 | /* generates a random number on [0,1]-real-interval */ |
174 | | -MersenneTwister.prototype.genrand_real1 = function() { |
175 | | - return this.genrand_int32()*(1.0/4294967295.0); |
176 | | - /* divided by 2^32-1 */ |
177 | | -} |
| 186 | +MersenneTwister.prototype.genrand_real1 = function () { |
| 187 | + return this.genrand_int32() * (1.0 / 4294967295.0); |
| 188 | + /* divided by 2^32-1 */ |
| 189 | +}; |
178 | 190 |
|
179 | 191 | /* generates a random number on [0,1)-real-interval */ |
180 | | -MersenneTwister.prototype.random = function() { |
181 | | - return this.genrand_int32()*(1.0/4294967296.0); |
| 192 | +MersenneTwister.prototype.random = function () { |
| 193 | + return this.genrand_int32() * (1.0 / 4294967296.0); |
182 | 194 | /* divided by 2^32 */ |
183 | | -} |
184 | | - |
| 195 | +}; |
| 196 | + |
185 | 197 | /* generates a random number on (0,1)-real-interval */ |
186 | | -MersenneTwister.prototype.genrand_real3 = function() { |
187 | | - return (this.genrand_int32() + 0.5)*(1.0/4294967296.0); |
| 198 | +MersenneTwister.prototype.genrand_real3 = function () { |
| 199 | + return (this.genrand_int32() + 0.5) * (1.0 / 4294967296.0); |
188 | 200 | /* divided by 2^32 */ |
189 | | -} |
190 | | - |
| 201 | +}; |
| 202 | + |
191 | 203 | /* generates a random number on [0,1) with 53-bit resolution*/ |
192 | | -MersenneTwister.prototype.genrand_res53 = function() { |
193 | | - var a=this.genrand_int32()>>>5, b=this.genrand_int32()>>>6; |
194 | | - return(a*67108864.0+b)*(1.0/9007199254740992.0); |
195 | | -} |
| 204 | +MersenneTwister.prototype.genrand_res53 = function () { |
| 205 | + var a = this.genrand_int32() >>> 5, |
| 206 | + b = this.genrand_int32() >>> 6; |
| 207 | + return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0); |
| 208 | +}; |
196 | 209 |
|
197 | 210 | /* These real versions are due to Isaku Wada, 2002/01/09 added */ |
198 | | -if(typeof exports == 'undefined'){ |
| 211 | +if (typeof exports == "undefined") { |
199 | 212 | var root = this; |
200 | 213 | } else { |
201 | 214 | var root = exports; |
|
0 commit comments