Deterministic linear congruential pseudo-random generator with one mutable seed and three derived outputs.
State evolution
RandomLCG
- Copies hold independent seed values; destruction requires no special resource handling.
- All four generator methods require a mutable receiver, for example
@r.nextSeed.
Fields
Generator outputs are numeric values, not views into seed; copied generators retain independent state.
Methods
- Advances the seed once, converts it to
Real32, and multiplies by 2−32. The result is in [0,1], including 1.0 because ofReal32rounding.
Known issue: getr32 can return exactly 1.0 because the seed is converted to Real32 before scaling; seed 1298181734 advances to 4294967295 and yields 1.0.
- Converts bound to
Nat64and returns the upper 32 bits of theNat64product of bound and the updatedNat32seed;Nat64overflow wraps. - For an integer bound b in 0 through 2^32, the unsigned result is floor(b × updatedSeed / 2^32). It is below b when b is positive; bound 0 returns 0 but still advances the state.
- Bounds are not checked against an intended range. Prefer nonnegative integer bounds. Other convertible inputs use conversion semantics. See the value-ownership rule.
- Converts bound to
Nat64and returns the upper 32 bits of theNat64product of bound and the updatedNat32seed, then casts that unsigned result toInt32;Nat64overflow wraps. - For an integer bound b in 0 through 2^32, the unsigned result is floor(b × updatedSeed / 2^32). It is below b when b is positive; bound 0 returns 0 but still advances the state.
- Nonnegative integer bounds no greater than 2^31 keep the result nonnegative; larger bounds can yield negative
Int32values. The method does not validate an intended bound range. Other convertible inputs use conversion semantics. See the value-ownership rule.
Generator step
nextSeed updates the Nat32 seed with seed × 0x8088405 + 1 modulo Nat32.
Determinism and replay
Reassigning the same initial seed reproduces the same output sequence. Replay requires the same initial seed and the same method/bound sequence.
Examples
Initial sequence
"RandomLCG" use
"control" use
{} () {} [
r: RandomLCG;
@r.nextSeed printStack _:;
@r.nextSeed printStack _:;
@r.getr32 printStack _:;
] "main" exportFunction
Expected Output During Compilation
1n32
134775814n32
0.86105r32
State progression checks
"RandomLCG" use
"String" use
"control" use
{} Int32 {} [
r: RandomLCG;
(@r.nextSeed 1n32 = LF
@r.nextSeed 134775814n32 = LF
@r.getr32 0.8610484600067139r32 = LF) printList
0
] "main" exportFunction
Expected Output
TRUE
TRUE
TRUE
Runtime sequence
"RandomLCG" use
"String" use
"control" use
{} Int32 {} [
r: RandomLCG;
("seed1=" @r.nextSeed LF
"seed2=" @r.nextSeed LF
"r32=" @r.getr32 LF) printList
0
] "main" exportFunction
Expected Output
seed1=1
seed2=134775814
r32=0.86105
See also
- murmurHash: Murmur3 32-bit hashing helpers over raw byte storage.
- sha1: Whole-input and incremental SHA-1 hashing helpers.