-
Notifications
You must be signed in to change notification settings - Fork 1
/
random.ts
100 lines (94 loc) · 2.7 KB
/
random.ts
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
class Random {
_normal: null | number = null;
next() {
return 0 | 255 * this.uniform();
}
random() {
return this.uniform();
}
uniform() {
return Math.random();
}
normal() {
if (null != this._normal) {
var b = this._normal;
this._normal = null;
return b;
}
b = this.uniform() || Math.pow(2, -53);
b = Math.sqrt(-2 * Math.log(b));
var c = 2 * Math.PI * this.uniform();
this._normal = b * Math.sin(c);
return b * Math.cos(c);
}
range(b: number, c: number) {
if (null == b) {
return this.uniform();
}
null == c && (c = b, b = 0);
return b + Math.floor(this.uniform() * (c - b));
}
exp() {
return -Math.log(this.uniform() || Math.pow(2, -53));
}
poisson(b: number) {
b = Math.exp(-(b || 1));
for (var c = 0, e = 1; e > b;) {
e *= this.uniform(), c++;
}
return c - 1;
}
gamma(b: number) {
var c;
var e = (1 > b ? 1 + b : b) - 1 / 3;
for (c = 1 / Math.sqrt(9 * e); ;) {
for (; ;) {
var a = this.normal();
var d = Math.pow(1 + c * a, 3);
if (0 < d) {
break;
}
}
var f = this.uniform();
if (f >= 1 - 0.331 * Math.pow(a, 4) && Math.log(f) >= 0.5 * Math.pow(a, 2) + e * (1 - d + Math.log(d))) {
break;
}
}
return 1 < b ? e * d : e * d * Math.exp(this.exp() / -b);
}
};
class MT extends Random {
_index: number
_state: number[]
_seed: number
constructor(a: number) {
super()
this._index = 0;
this._state = Array(624);
this._state[0] = null != a ? a : 4294967295 * Math.random() | 0;
this._seed = this._state[0];
var d = this._state;
for (a = 1; 624 > a; a++) {
d[a] = d[a - 1] ^ d[a - 1] >>> 30, d[a] = 1812433253 * d[a] + a, d[a] &= (d[a] << 32) - 1;
}
}
_generateNumbers() {
var a, d = this._state;
for (a = 0; 624 > a; a++) {
var f = d[a] & 2147483648;
f += d[(a + 1) % 624] & 2147483647;
d[a] = d[(a + 397) % 624] ^ f >>> 1;
0 !== f % 2 && (d[a] ^= 2567483615);
}
};
uniform() {
0 === this._index && this._generateNumbers();
var a = this._state[this._index];
a ^= a >>> 11;
a ^= a << 7 & 2636928640;
a ^= a << 15 & 4022730752;
this._index = (this._index + 1) % 624;
return 1.0 / 4294967296.0 * ((a ^ a >>> 18) >>> 0);
};
}
export { Random, MT }