SeedRandom
Seeded random number generator to get reproducible sequences of values.
SeedRandom provides a deterministic pseudo-random number generator that produces the same sequence of random values for a given seed. This is essential for:
- Procedural generation that needs to be reproducible
- Multiplayer games requiring synchronized random events
- Testing scenarios that need predictable randomness
- Save/load systems that need to recreate random sequences
The implementation uses the Park Miller (1988) "minimal standard" linear congruential generator algorithm: (seed * 16807) % 2147483647
Example usage:
// Create a seeded random generator
var rng = new SeedRandom(12345);
// Generate random values
var randomFloat = rng.random(); // [0, 1)
var randomInt = rng.between(1, 100); // [1, 100)
// Shuffle an array deterministically
var items = [1, 2, 3, 4, 5];
rng.shuffle(items);
// Reset to initial seed to replay sequence
rng.reset();
var sameFloat = rng.random(); // Same as first randomFloat
Note: This is not cryptographically secure and should not be used for security-sensitive applications.
Instance Members
seed: Float
The current seed value. This value changes with each random number generation.
initialSeed: Float
The initial seed value used when creating this generator. Used by reset() to restore the original sequence.
Shuffle an Array in place using the Fisher-Yates algorithm.
This operation modifies the original array. The shuffle is deterministic based on the current seed state, so the same seed will always produce the same shuffle order.
Example:
var deck = ["A", "K", "Q", "J", "10", "9", "8", "7"];
rng.shuffle(deck);
// deck is now shuffled in a reproducible way
Name | Type | Description |
---|---|---|
arr |
Array<shuffle.T> | The array to shuffle. Modified in place. |
random(): Float
Returns a pseudo-random float in the range [0, 1).
The value is uniformly distributed and will be >= 0 and < 1. Each call advances the internal seed state.
Returns | Description |
---|---|
Float | A pseudo-random float between 0 (inclusive) and 1 (exclusive) |
Returns a pseudo-random integer in the range [min, max).
The value will be >= min and < max. The distribution is uniform across the range.
Example:
var diceRoll = rng.between(1, 7); // 1-6
var percent = rng.between(0, 101); // 0-100
Name | Type | Description |
---|---|---|
min |
Int | The minimum value (inclusive) |
max |
Int | The maximum value (exclusive) |
Returns | Description |
---|---|
Int | A pseudo-random integer in the specified range |
Resets the generator to its initial seed value.
This allows replaying the same sequence of random values. Optionally, a new initial seed can be provided.
Example:
var rng = new SeedRandom(100);
var a = rng.random();
var b = rng.random();
rng.reset(); // Back to seed 100
var a2 = rng.random(); // Same as 'a'
var b2 = rng.random(); // Same as 'b'
rng.reset(200); // Change to new seed
Name | Type | Default | Description |
---|---|---|---|
initialSeed |
Float | (optional) | Optional new seed to use. If not provided, resets to the original seed from construction. |
Creates a new seeded random number generator.
Name | Type | Description |
---|---|---|
seed |
Float | The seed value. Must be a positive number. Same seed values will produce identical random sequences. |