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

import std.random;

fn main() {
    random.seed(42);
    println(random.random());   // ≈ 0.6394267984578837
    println(random.randint(0, 10));
}

Rng: independent seeded generators

The module-level functions above (seed, random, randint, shuffle, choices, gauss) share one implicit generator per thread. Rng is a plain value a caller holds and passes around instead, so several independent streams can run in the same thread. It uses xoshiro256++ (see hew-runtime/src/random.rs for the choice rationale) rather than MT19937, so its output does not match the module-level functions for the same seed.

import std.random;

fn main() {
    let rng = random.Rng.new(42);
    println(rng.next_u64());
    println(rng.next_f64());
    println(rng.range(0, 10));
}

Contents

Functions

Function crypto_bytes

pub fn crypto_bytes(n: i64) -> bytes

Return n cryptographically secure random bytes drawn from OS entropy.

Function crypto_u64

pub fn crypto_u64() -> u64

Return a cryptographically secure random u64 drawn from OS entropy.

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).

Types

Struct Rng

An independent, seedable pseudo-random generator (xoshiro256++).

Unlike the module-level functions, an Rng value is not shared thread-local state: create one with Rng.new(seed) and hold it wherever it is needed. Two Rng values created with the same seed produce identical sequences.

Fields

state: Vec<u64>

Traits

Trait RngMethods

Methods available on an Rng.

Methods

fn next_u64(self: Self) -> u64
fn next_f64(self: Self) -> f64
fn range(self: Self, lo: i64, hi: i64) -> i64
fn shuffle(self: Self, v: Vec<i64>)