-
Notifications
You must be signed in to change notification settings - Fork 14
/
MRG32k3a.js
81 lines (73 loc) · 1.91 KB
/
MRG32k3a.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
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
// From http://baagoe.com/en/RandomMusings/javascript/
function MRG32k3a() {
return (function(args) {
// Copyright (c) 1998, 2002 Pierre L'Ecuyer, DIRO, Université de Montréal.
// http://www.iro.umontreal.ca/~lecuyer/
var m1 = 4294967087;
var m2 = 4294944443;
var s10 = 12345,
s11 = 12345,
s12 = 123,
s20 = 12345,
s21 = 12345,
s22 = 123;
if (args.length === 0) {
args = [+new Date()];
}
var mash = Mash();
for (var i = 0; i < args.length; i++) {
s10 += mash(args[i]) * 0x100000000; // 2 ^ 32
s11 += mash(args[i]) * 0x100000000;
s12 += mash(args[i]) * 0x100000000;
s20 += mash(args[i]) * 0x100000000;
s21 += mash(args[i]) * 0x100000000;
s22 += mash(args[i]) * 0x100000000;
}
s10 %= m1;
s11 %= m1;
s12 %= m1;
s20 %= m2;
s21 %= m2;
s22 %= m2;
mash = null;
var uint32 = function() {
var m1 = 4294967087;
var m2 = 4294944443;
var a12 = 1403580;
var a13n = 810728;
var a21 = 527612;
var a23n = 1370589;
var k, p1, p2;
/* Component 1 */
p1 = a12 * s11 - a13n * s10;
k = p1 / m1 | 0;
p1 -= k * m1;
if (p1 < 0) p1 += m1;
s10 = s11;
s11 = s12;
s12 = p1;
/* Component 2 */
p2 = a21 * s22 - a23n * s20;
k = p2 / m2 | 0;
p2 -= k * m2;
if (p2 < 0) p2 += m2;
s20 = s21;
s21 = s22;
s22 = p2;
/* Combination */
if (p1 <= p2) return p1 - p2 + m1;
else return p1 - p2;
};
var random = function() {
return uint32() * 2.3283064365386963e-10; // 2^-32
};
random.uint32 = uint32;
random.fract53 = function() {
return random() +
(uint32() & 0x1fffff) * 1.1102230246251565e-16; // 2^-53
};
random.version = 'MRG32k3a 0.9';
random.args = args;
return random;
} (Array.prototype.slice.call(arguments)));
};