blob: 392336e6f38d5bf9641ca0b2dd6509a32eec05d1 (
plain)
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
190
|
// Based on: https://github.com/dubzzz/pure-rand/blob/5da623a4eebf47f01c5b87c43c9fb4d43ad949bd/src/generator/MersenneTwister.ts
const N = 624;
const M = 397;
const R = 31;
const A = 0x9908b0df;
const F = 1812433253;
const U = 11;
const S = 7;
const B = 0x9d2c5680;
const T = 15;
const C = 0xefc60000;
const L = 18;
const MASK_LOWER = 2 ** R - 1;
const MASK_UPPER = 2 ** R;
const HIGH_MULTIPLIER_53 = 67108864.0;
const FLOATIFY_32 = 1.0 / 4294967296.0;
const FLOATIFY_53 = 1.0 / 9007199254740992.0;
const { imul, trunc } = Math;
/**
* Generates the random state from the given number or array.
*
* @param seed The seed to generate the random state from.
*/
function seedFrom(seed: number | number[]): number[] {
return typeof seed === 'number' ? numberSeeded(seed) : arraySeeded(seed);
}
/**
* Generates the random state from the given number.
*
* @param seed The seed to generate the random state from.
*/
function numberSeeded(seed: number): number[] {
const out = Array.from<number>({ length: N });
out[0] = seed;
for (let idx = 1; idx !== N; ++idx) {
const xored = out[idx - 1] ^ (out[idx - 1] >>> 30);
out[idx] = trunc(imul(F, xored) + idx);
}
return out;
}
/**
* Generates the random state from the given array.
*
* @param seed The seed to generate the random state from.
*/
function arraySeeded(seed: number[]): number[] {
const out = numberSeeded(19650218);
let idxOut = 1;
let idxSeed = 0;
for (let iteration = Math.max(N, seed.length); iteration !== 0; --iteration) {
const xored = out[idxOut - 1] ^ (out[idxOut - 1] >>> 30);
out[idxOut] = trunc(
(out[idxOut] ^ imul(xored, 1664525)) + seed[idxSeed] + idxSeed
);
idxOut++;
idxSeed++;
if (idxOut >= N) {
out[0] = out[N - 1];
idxOut = 1;
}
if (idxSeed >= seed.length) {
idxSeed = 0;
}
}
for (let iteration = N - 1; iteration !== 0; iteration--) {
out[idxOut] = trunc(
(out[idxOut] ^
imul(out[idxOut - 1] ^ (out[idxOut - 1] >>> 30), 1566083941)) -
idxOut
);
idxOut++;
if (idxOut >= N) {
out[0] = out[N - 1];
idxOut = 1;
}
}
out[0] = 0x80000000;
return out;
}
/**
* Twists the given randomness states.
*
* @param states The randomness states to twist.
*
* @returns The given states.
*/
function twist(states: number[]): number[] {
for (let idx = 0; idx !== N - M; ++idx) {
const y = (states[idx] & MASK_UPPER) + (states[idx + 1] & MASK_LOWER);
states[idx] = states[idx + M] ^ (y >>> 1) ^ (-(y & 1) & A);
}
for (let idx = N - M; idx !== N - 1; ++idx) {
const y = (states[idx] & MASK_UPPER) + (states[idx + 1] & MASK_LOWER);
states[idx] = states[idx + M - N] ^ (y >>> 1) ^ (-(y & 1) & A);
}
const y = (states[N - 1] & MASK_UPPER) + (states[0] & MASK_LOWER);
states[N - 1] = states[M - 1] ^ (y >>> 1) ^ (-(y & 1) & A);
return states;
}
/**
* Mersenne Twister based random number generator.
*/
export class MersenneTwister19937 {
/**
* Creates a new Mersenne Twister random number generator based on the given seed.
*
* @param seed The seed to generate the random state from. Defaults to a random seed.
*/
constructor(seed?: number | number[]);
/**
* Creates a new Mersenne Twister random number generator based on the given seed.
*
* @param seed The seed to generate the random state from. Defaults to a random seed.
* @param states The states to use, must be an array of 624 32-bit integers.
* @param index The index to use, must be a number between 0 and 623.
*/
constructor(
seed: number | number[] = Math.random() * Number.MAX_SAFE_INTEGER,
private states: number[] = twist(seedFrom(seed)),
private index: number = 0
) {}
/**
* Generates the next 32-bit unsigned integer.
*/
nextU32(): number {
let y = this.states[this.index];
y ^= this.states[this.index] >>> U;
y ^= (y << S) & B;
y ^= (y << T) & C;
y ^= y >>> L;
if (++this.index >= N) {
this.states = twist(this.states);
this.index = 0;
}
return y >>> 0;
}
/**
* Generates the next 32-bit float.
*/
nextF32(): number {
return this.nextU32() * FLOATIFY_32;
}
/**
* Generates the next 53-bit unsigned integer.
*/
nextU53(): number {
const high = this.nextU32() >>> 5;
const low = this.nextU32() >>> 6;
return high * HIGH_MULTIPLIER_53 + low;
}
/**
* Generates the next 53-bit float.
*/
nextF53(): number {
return this.nextU53() * FLOATIFY_53;
}
/**
* Sets the seed of the random number generator.
*
* @param seed The seed to use.
*/
seed(seed: number | number[]): void {
this.states = twist(seedFrom(seed));
this.index = 0;
}
}
|