Module std::random

Seedable pseudo-random number generation.

Backed by a CPython-compatible MT19937 Mersenne Twister implemented in hew-runtime/src/random.rs. State is thread-local — one generator per thread. A fixed seed produces a deterministic, reproducible sequence that matches CPython's random module output for the same seed.

Examples

fn main() {
    random.seed(42);
    let r = random.random();   // ≈ 0.6394267984578837
    let n = random.randint(0, 10);
}

Contents

Functions

Function seed

pub fn seed(s: i64)

Seed the PRNG so subsequent calls produce a deterministic sequence.

The seed is reduced to its low 32 bits and passed to CPython's init_by_array. Calling seed with the same value always produces the same stream.

Function random

pub fn random() -> f64

Return a random float in [0.0, 1.0) with 53-bit precision.

Uses CPython's random() algorithm (two 27/26-bit MT words combined).

Function gauss

pub fn gauss(mean: f64, sigma: f64) -> f64

Return a random float drawn from a Gaussian (normal) distribution with the given mean and sigma (standard deviation).

Uses the Box-Muller method with spare-value caching, matching CPython's random.gauss implementation.

Function randint

pub fn randint(lo: i64, hi: i64) -> i64

Return a random integer in the half-open range [lo, hi).

Returns lo when hi <= lo.

Function shuffle

pub fn shuffle(v: Vec<i64>)

Shuffle a Vec<i64> in-place using Fisher-Yates.

Matches CPython's random.shuffle output for the same seed.

Function choices

pub fn choices(weights: Vec<f64>, total: f64, n: i64) -> i64

Weighted choice by bisecting cumulative weights. Returns the chosen index.

weights is a Vec<f64> of cumulative weights (each entry is the sum of all weights up to and including that index), total is the grand total, and n is reserved for future multi-sample use (pass 1).