-
Notifications
You must be signed in to change notification settings - Fork 14
/
Xorshift03.js
48 lines (44 loc) · 1.26 KB
/
Xorshift03.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
// From http://baagoe.com/en/RandomMusings/javascript/
function Xorshift03() {
return (function(args) {
// George Marsaglia, 13 May 2003
// http://groups.google.com/group/comp.lang.c/msg/e3c4ea1169e463ae
var x = 123456789,
y = 362436069,
z = 521288629,
w = 88675123,
v = 886756453;
if (args.length == 0) {
args = [+new Date];
}
var mash = Mash();
for (var i = 0; i < args.length; i++) {
x ^= mash(args[i]) * 0x100000000; // 2^32
y ^= mash(args[i]) * 0x100000000;
z ^= mash(args[i]) * 0x100000000;
v ^= mash(args[i]) * 0x100000000;
w ^= mash(args[i]) * 0x100000000;
}
mash = null;
var uint32 = function() {
var t = (x ^ (x >>> 7)) >>> 0;
x = y;
y = z;
z = w;
w = v;
v = (v ^ (v << 6)) ^ (t ^ (t << 13)) >>> 0;
return ((y + y + 1) * v) >>> 0;
}
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 = 'Xorshift03 0.9';
random.args = args;
return random;
} (Array.prototype.slice.call(arguments)));
};