std/random

March 13, 2026 ยท View on GitHub

The std/random module provides an idiomatic, object-oriented pseudo-random number generator (PRNG) wrapper around POSIX <stdlib.h> functions.

Usage

import "std/random.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 "Rolled: {bounded}";
}

Struct Definition

struct Random {
    seed: U32;
}

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.

Generation

MethodSignatureDescription
next_intnext_int(self) -> intReturns a random integer in raw range [0, RAND_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.