std/random

The std/random module provides an idiomatic, object-oriented pseudo-random number generator (PRNG). Each Random owns an independent 64-bit xorshift64* state, so generators seeded the same way produce identical deterministic sequences without interfering with one another (unlike a process-global srand/rand).

Usage

import "std/random.zc"
import "std/io.zc"

fn main() {
    // Automatically seeds the generator with the current time
    let rng = Random::new();

    // Generate random integers
    let bounded = rng.next_int_range(1, 100); // 1 to 100 inclusive
    
    println(f"Rolled: {bounded}");
}

Struct Definition

struct Random {
    state: u64;
}

Methods

Initialization

MethodSignatureDescription
newRandom::new() -> RandomCreates a new random generator seeded with current system time.
from_seedRandom::from_seed(seed: U32) -> RandomCreates a new random generator using a specific seed. Identical seeds produce identical sequences.

Generation

MethodSignatureDescription
next_rawnext_raw(self) -> u64Advances the generator and returns the next raw 64-bit value.
next_intnext_int(self) -> intReturns a random non-negative integer in [0, INT32_MAX].
next_int_rangenext_int_range(self, min: int, max: int) -> intReturns a random integer in range [min, max] inclusive.
next_doublenext_double(self) -> doubleReturns a random floating-point number in range [0.0, 1.0).
next_boolnext_bool(self) -> boolReturns a random boolean.