Skip to content

Instantly share code, notes, and snippets.

@alenaksu
Last active November 14, 2024 17:15
Show Gist options
  • Save alenaksu/b8518f0f52840be2e58307ec860f014c to your computer and use it in GitHub Desktop.
Save alenaksu/b8518f0f52840be2e58307ec860f014c to your computer and use it in GitHub Desktop.
PRNG XORShift32
function *XORShift32(seed: number | bigint): Generator<Number> {
const base = 2 ** 32;
seed = BigInt(seed);
while(true) {
let x = seed as bigint;
x = BigInt.asUintN(32, x);
x ^= x << 13n;
x ^= x >> 17n;
x ^= x << 5n;
x = BigInt.asUintN(32, x);
yield Number((seed = x)) / base;
};
};
const rnd = XORShift32(1239810);
for (let i = 0; i < 100; i++) {
console.log(rnd.next().value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment