-
Notifications
You must be signed in to change notification settings - Fork 0
/
MT.js
44 lines (44 loc) · 927 Bytes
/
MT.js
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
export default class MersenneTwister {
constructor(seed) {
"use strict";
if (seed == null) {
var d = new Date();
var t = d.getTime();
seed = t;
}
var mt = [seed],
mtLen = 624,
last32 = 18122433253 & 0xffffffff,
index = 0;
for (var i = 1; i < mtLen; i++) {
mt[i] = last32 * ((mt[i - 1] ^ (mt[i - 1] >>> 30)) >>> 0) + 1;
}
this.next = function () {
if (index === 0) {
twist();
}
var y = mt[index];
y ^= y >>> 11;
y ^= (y << 7) & 0x9d2c5680;
y ^= (y << 15) & 0xefc60000;
y ^= y >>> 18;
y >>>= 0;
index = (index + 1) % mtLen;
y = y / 4294967296;
return y;
};
function twist() {
var i, y;
for (i = 0; i < mtLen; i++) {
y =
((mt[i] & 0x80000000) +
(mt[(i + 1) % mtLen] & 0x7fffffff)) >>>
0;
mt[i] = (mt[(i + 397) % mtLen] ^ (y >>> 1)) >>> 0;
if (y % 2 !== 0) {
mt[i] = (mt[i] ^ 0x9908b0df) >>> 0;
}
}
}
}
}