Seed Randomizer
SeedRandomizer is a deterministic pseudo-random generator based on the Linear Congruential Generator (LCG) algorithm. It normalizes the seed to three decimal places, ensuring that even fractional inputs yield repeatable sequences.
Consistent Game Behavior
For example, you can use numeric session/round identifiers as a seed. In this case, all random values will be the same for the same session/round. It's beneficial for consistent game behavior when restoring or replaying the round.
function createSymbolsList(seed:number):number[] {
const randomizer = new SeedRandomizer(seed);
randomizer.replaceMath();
const symbols:number[] = [];
for (let index = 0; index < 3; index++) {
symbols.push(RandomUtil.randomBetweenInt(0, 10));
}
randomizer.restoreMath();
return symbols;
}
const listA = createSymbolsList(25252);
console.log(listA);
// Output: [ 0, 6, 5 ]
const listB = createSymbolsList(25252);
console.log(listB);
// Output: [ 0, 6, 5 ]
const listC = createSymbolsList(54321);
console.log(listC);
// Output: [ 2, 4, 7 ]
create
Creates a new instance of the SeendRandomizer with a seed generated using the original Math.random.
static create():SeedRandomizer
const tempRandomizer = SeedRandomizer.create();
console.log(tempRandomizer.random());
// Output: random value between 0 and 1, for example, 0.4896780123
replaceMath / restoreMath
Overrides Math.random so all standard function calls route through the seeded generator, and restores the original implementation when no longer needed.
replaceMath():void
restoreMath():void
const randomizer = new SeedRandomizer(25852);
randomizer.replaceMath();
const seededValue = RandomUtil.randomBetweenInt(0, 10);
console.log(seededValue);
// Output: 2
randomizer.restoreMath();
const randomValue = Math.random();
console.log(randomValue);
// Output: random value between 0 and 1, for example, 0.0904787782
random
Generates the next pseudo-random value between 0 and 1.
random():number
const randomizer = new SeedRandomizer(151515);
console.log(randomizer.random());
// Output: 0.9560739828637042
console.log(randomizer.random());
// Output: 0.2823941801400841