-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
39 lines (33 loc) · 945 Bytes
/
mod.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
// constants
const MUL = 6364136223846793005n;
const ALT = 9843272141302948723n;
// wrap value to specified size
const clamp64 = (n: bigint) => BigInt.asUintN(64, n);
const clamp32 = (n: bigint) => BigInt.asUintN(32, n);
/**
* Generate seeded psuedorandom numbers in sequence.
*
* Based on [PCG32](https://www.pcg-random.org)
* @param seed seed to use for generating the psuedorandom sequence
*/
export function* seededNumbers(seed: number | bigint) {
let rng = BigInt(seed);
let value = 0n;
let added = (rng << 1n) | 1n;
const next = () => {
let old = value;
value = clamp64(old * MUL + added);
let xorshifted = clamp32(((old >> 18n) ^ old) >> 27n);
let rotation = clamp32(old >> 59n);
let result = clamp32(
(xorshifted >> rotation) | (xorshifted << (-rotation & 31n)),
);
return Number(result);
};
next();
value = value + rng + ALT;
next();
while (true) {
yield next();
}
}