viva_aion/rng

RNG - Deterministic pseudo-random number generation

“All You Zombies” - The seed determines the entire history. Same seed = same universe. Always.

Uses a simple LCG (Linear Congruential Generator) for cross-platform determinism.

Types

RNG state

pub type Rng {
  Rng(state: Int)
}

Constructors

  • Rng(state: Int)

Values

pub fn from_string(seed: String) -> Rng

Create RNG from string seed (hash it)

pub fn new(seed: Int) -> Rng

Create RNG from seed

pub fn next(rng: Rng) -> #(Int, Rng)

Generate next random number, returns (value, new_rng)

pub fn next_float(rng: Rng) -> #(Float, Rng)

Generate random float in range [0.0, 1.0)

pub fn next_int(rng: Rng, max: Int) -> #(Int, Rng)

Generate random integer in range [0, max)

pub fn next_range(rng: Rng, min: Int, max: Int) -> #(Int, Rng)

Generate random integer in range [min, max]

pub fn pick(rng: Rng, items: List(a)) -> #(Result(a, Nil), Rng)

Pick random element from list

pub fn shuffle(rng: Rng, items: List(a)) -> #(List(a), Rng)

Shuffle a list deterministically - O(N log N) Uses sort-by-random-key instead of Fisher-Yates for better performance

Search Document