-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.ts
189 lines (159 loc) · 4.94 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
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
/*
A standalone, pure Typescript implementation of the Mersenne Twister pseudo random number generator compatible with Deno. MIT license
Based on:
https://github.com/pigulla/mersennetwister
https://gist.github.com/banksean/300494
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c
*/
const MAX_INT = 4294967296.0,
N = 624,
M = 397,
UPPER_MASK = 0x80000000,
LOWER_MASK = 0x7fffffff,
MATRIX_A = 0x9908b0df;
export default class MersenneTwister {
/**
* Instantiates a new Mersenne Twister.
* @param {number=} seed The initial seed value.
*/
mt: number[]
mti: number
constructor(seed: number) {
this.mt = new Array(N)
this.mti = N + 1
this.seed(seed)
}
static dateSeed() {
return new Date().getTime()
}
/**
* Initializes the state vector by using one unsigned 32-bit integer "seed", which may be zero.
* @param {number} seed The seed value.
*/
seed(seed: number) {
let 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.
* @param {array} vector The seed vector.
*/
seedArray(vector: number[]) {
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.
* @returns {number}
*/
int() {
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 real in the interval [0;1[ with 32-bit resolution.
* Equivelant to Math.random()
* @returns {number}
*/
random() {
return this.int() * (1.0 / MAX_INT);
}
/**
* Generates a random unsigned 31-bit integer.
*
* @since 0.1.0
* @returns {number}
*/
int31() {
return this.int() >>> 1;
}
/**
* Generates a random real in the interval [0;1] with 32-bit resolution.
*
* @since 0.1.0
* @returns {number}
*/
real() {
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}
*/
realx() {
return (this.int() + 0.5) * (1.0 / MAX_INT);
}
/**
* Generates a random real in the interval [0;1[ with 53-bit resolution.
*
* @returns {number}
*/
rndHiRes() {
var a = this.int() >>> 5,
b = this.int() >>> 6;
return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0);
}
}