-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmersenne-twister.js
More file actions
243 lines (210 loc) · 5.78 KB
/
mersenne-twister.js
File metadata and controls
243 lines (210 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
(function (root, factory) {
'use strict';
if (typeof exports === 'object') {
module.exports = factory();
} else if (typeof define === 'function' && define.amd) {
define(factory);
} else {
root.MersenneTwister = factory();
}
}(this, function () {
/**
* A standalone, pure JavaScript implementation of the Mersenne Twister pseudo random number generator. Compatible
* with Node.js, requirejs and browser environments. Packages are available for npm, Jam and Bower.
*
* @module MersenneTwister
* @author Raphael Pigulla <pigulla@four66.com>
* @license See the attached LICENSE file.
* @version 0.2.3
*/
/*
* Most comments were stripped from the source. If needed you can still find them in the original C code:
* http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c
*
* The original port to JavaScript, on which this file is based, was done by Sean McCullough. It can be found at:
* https://gist.github.com/banksean/300494
*/
'use strict';
var MAX_INT = 4294967296.0,
N = 624,
M = 397,
UPPER_MASK = 0x80000000,
LOWER_MASK = 0x7fffffff,
MATRIX_A = 0x9908b0df;
/**
* Instantiates a new Mersenne Twister.
*
* @constructor
* @alias module:MersenneTwister
* @since 0.1.0
* @param {number=} seed The initial seed value.
*/
var MersenneTwister = function (seed) {
if (typeof seed === 'undefined') {
seed = new Date().getTime();
}
this.mt = new Array(N);
this.mti = N + 1;
this.seed(seed);
};
/**
* Initializes the state vector by using one unsigned 32-bit integer "seed", which may be zero.
*
* @since 0.1.0
* @param {number} seed The seed value.
*/
MersenneTwister.prototype.seed = function (seed) {
var s;
this.mt[0] = seed >>> 0;
for (this.mti = 1; this.mti < N; this.mti++) {
s = this.mt[this.mti - 1] ^ (this.mt[this.mti - 1] >>> 30);
this.mt[this.mti] =
(((((s & 0xffff0000) >>> 16) * 1812433253) << 16) + (s & 0x0000ffff) * 1812433253) + this.mti;
this.mt[this.mti] >>>= 0;
}
};
/**
* Initializes the state vector by using an array key[] of unsigned 32-bit integers of the specified length. If
* length is smaller than 624, then each array of 32-bit integers gives distinct initial state vector. This is
* useful if you want a larger seed space than 32-bit word.
*
* @since 0.1.0
* @param {array} vector The seed vector.
*/
MersenneTwister.prototype.seedArray = function (vector) {
var i = 1,
j = 0,
k = N > vector.length ? N : vector.length,
s;
this.seed(19650218);
for (; k > 0; k--) {
s = this.mt[i-1] ^ (this.mt[i-1] >>> 30);
this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1664525) << 16) + ((s & 0x0000ffff) * 1664525))) +
vector[j] + j;
this.mt[i] >>>= 0;
i++;
j++;
if (i >= N) {
this.mt[0] = this.mt[N - 1];
i = 1;
}
if (j >= vector.length) {
j = 0;
}
}
for (k = N - 1; k; k--) {
s = this.mt[i - 1] ^ (this.mt[i - 1] >>> 30);
this.mt[i] =
(this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1566083941) << 16) + (s & 0x0000ffff) * 1566083941)) - i;
this.mt[i] >>>= 0;
i++;
if (i >= N) {
this.mt[0] = this.mt[N - 1];
i = 1;
}
}
this.mt[0] = 0x80000000;
};
/**
* Generates a random unsigned 32-bit integer.
*
* @since 0.1.0
* @returns {number}
*/
MersenneTwister.prototype.int = function () {
var y,
kk,
mag01 = new Array(0, MATRIX_A);
if (this.mti >= N) {
if (this.mti === N + 1) {
this.seed(5489);
}
for (kk = 0; kk < N - M; kk++) {
y = (this.mt[kk] & UPPER_MASK) | (this.mt[kk + 1] & LOWER_MASK);
this.mt[kk] = this.mt[kk + M] ^ (y >>> 1) ^ mag01[y & 1];
}
for (; kk < N - 1; kk++) {
y = (this.mt[kk] & UPPER_MASK) | (this.mt[kk + 1] & LOWER_MASK);
this.mt[kk] = this.mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 1];
}
y = (this.mt[N - 1] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
this.mt[N - 1] = this.mt[M - 1] ^ (y >>> 1) ^ mag01[y & 1];
this.mti = 0;
}
y = this.mt[this.mti++];
y ^= (y >>> 11);
y ^= (y << 7) & 0x9d2c5680;
y ^= (y << 15) & 0xefc60000;
y ^= (y >>> 18);
return y >>> 0;
};
/**
* Generates a random unsigned 31-bit integer.
*
* @since 0.1.0
* @returns {number}
*/
MersenneTwister.prototype.int31 = function () {
return this.int() >>> 1;
};
/**
* Generates a random real in the interval [0;1] with 32-bit resolution.
*
* @since 0.1.0
* @returns {number}
*/
MersenneTwister.prototype.real = function () {
return this.int() * (1.0 / (MAX_INT - 1));
};
/**
* Generates a random real in the interval ]0;1[ with 32-bit resolution.
*
* @since 0.1.0
* @returns {number}
*/
MersenneTwister.prototype.realx = function () {
return (this.int() + 0.5) * (1.0 / MAX_INT);
};
/**
* Generates a random real in the interval [0;1[ with 32-bit resolution.
*
* @since 0.1.0
* @returns {number}
*/
MersenneTwister.prototype.rnd = function () {
return this.int() * (1.0 / MAX_INT);
};
/**
* Generates a random real in the interval [0;1[ with 32-bit resolution.
*
* Same as .rnd() method - for consistency with Math.random() interface.
*
* @since 0.2.0
* @returns {number}
*/
MersenneTwister.prototype.random = MersenneTwister.prototype.rnd;
/**
* Generates a random real in the interval [0;1[ with 53-bit resolution.
*
* @since 0.1.0
* @returns {number}
*/
MersenneTwister.prototype.rndHiRes = function () {
var a = this.int() >>> 5,
b = this.int() >>> 6;
return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0);
};
var instance = new MersenneTwister();
/**
* A static version of [rnd]{@link module:MersenneTwister#rnd} on a randomly seeded instance.
*
* @static
* @function random
* @memberof module:MersenneTwister
* @returns {number}
*/
MersenneTwister.random = function () {
return instance.rnd();
};
return MersenneTwister;
}));