Deterministic linear congruential pseudo-random generator with one mutable seed and three derived outputs.


State evolution


RandomLCG

RandomLCG

(-- generator) Constructs a new generator whose Nat32 seed is 0.
  • 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

nextSeed

(-- seed) Advances and returns the updated seed.

Remarks

getr32

(-- value) Maps the updated seed to a Real32 value.
  • Advances the seed once, converts it to Real32, and multiplies by 2−32. The result is in [0,1], including 1.0 because of Real32 rounding.

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.

getn32

(bound -- value) Advances the seed once and maps the updated state to a Nat32 using bound.
  • Converts bound to Nat64 and returns the upper 32 bits of the Nat64 product of bound and the updated Nat32 seed; Nat64 overflow 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.

geti32

(bound -- value) Advances the seed once and returns the high-product mapping as Int32.
  • Converts bound to Nat64 and returns the upper 32 bits of the Nat64 product of bound and the updated Nat32 seed, then casts that unsigned result to Int32; Nat64 overflow 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 Int32 values. 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