Skip to content

stdlib.rand

Random Number Generation

Generated from v0.60.1. 4 source files, 55 documented symbols.

chacha.xi

type ChaChaRng

ChaCha20-based deterministic RNG state.

Field Type
state Vec[UInt32]
pos Int
fn chacha_rng_new() -> ChaChaRng

Create a ChaCha RNG with an all-zero key (deterministic default). Test vector: the first output word of the zero-key block is 0xADE0B876.

fn chacha_rng_from_seed(seed: UInt64) -> ChaChaRng

Create a ChaCha RNG from a 64-bit seed. The seed's two 32-bit halves are replicated into the 8 key words.

fn chacha_rng_next_u32(r: &mut ChaChaRng) -> UInt32

Return the next 32-bit output word. pos tracks the position within the 64-word keystream window (16 words per block, 4 blocks per window); the block counter advances at each 16-word boundary.

fn chacha_rng_next_int(r: &mut ChaChaRng) -> Int

Return the next value as a signed i64 in [0, 2^32).

fn chacha_rng_next_float(r: &mut ChaChaRng) -> Float64

Return the next value as a Float64 in [0, 1).

fn chacha_rng_next_bounded(r: &mut ChaChaRng, hi: Int) -> Int

Return the next value in [0, hi). Requires hi > 0.




mt19937.xi

type Mt19937

Mersenne Twister (MT19937) RNG state.

Field Type
state Vec[UInt32]
index Int
fn mt19937_new() -> Mt19937

Create an MT19937 generator with the classic default seed 5489.

fn mt19937_from_seed(seed: UInt32) -> Mt19937

Create an MT19937 generator from an explicit 32-bit seed.

fn mt19937_reseed(r: &mut Mt19937, seed: UInt32)

Re-seed an existing generator in place using the classic seeding algorithm.

fn mt19937_next_u32(r: &mut Mt19937) -> UInt32

Return the next 32-bit output word, tempering with: y ^= y >> 11; y ^= (y << 7) & 0x9d2c5680; y ^= (y << 15) & 0xefc60000; y ^= y >> 18.

fn mt19937_next_int(r: &mut Mt19937) -> Int

Return the next value as a signed i64 in [0, 2^32).

fn mt19937_next_float(r: &mut Mt19937) -> Float64

Return the next value as a Float64 in [0, 1).

fn mt19937_next_bounded(r: &mut Mt19937, hi: Int) -> Int

Return the next value in [0, hi). Requires hi > 0.




pcg.xi

type Pcg

PCG64 RNG state.

Field Type
state UInt64
inc UInt64
fn pcg_new() -> Pcg

Create a PCG generator with the reference default state/stream constants.

fn pcg_from_seed(seed: UInt64) -> Pcg

Create a PCG generator from a 64-bit seed. Standard init: state = 0, inc = (seed << 1) | 1, then advance once.

fn pcg_next_u32(r: &mut Pcg) -> UInt32

Return the next 32-bit output word. xorshifted = ((oldstate >> 18) ^ oldstate) >> 27; rot = oldstate >> 59; output = (xorshifted >> rot) | (xorshifted << ((-rot) & 31)).

fn pcg_next_int(r: &mut Pcg) -> Int

Return the next value as a signed i64 in [0, 2^32).

fn pcg_next_float(r: &mut Pcg) -> Float64

Return the next value as a Float64 in [0, 1).

fn pcg_next_bounded(r: &mut Pcg, hi: Int) -> Int

Return the next value in [0, hi). Requires hi > 0.




rand.xi

type StdRng

=== Standard RNG ===

Field Type
state Int

Derives: Clone

fn new() -> StdRng

Seeded standard RNG (OS/time entropy when available).

  • Precondition: true

fn from_seed(seed: Int) -> StdRng

Deterministic standard RNG from an integer seed.

fn random() -> Float64

=== Basic random values ===

  • Postcondition: result >= 0
  • Postcondition: result < 1

fn random_int(min: Int, max: Int) -> Int

Uniform Int in [min, max] (inclusive).

  • Precondition: min <= max
  • Postcondition: result >= min && result <= max

fn random_float(min: Float64, max: Float64) -> Float64

Uniform Float64 in [min, max).

fn random_bool() -> Bool

Fair coin flip.

fn random_bytes(count: Int) -> Vec[UInt8]

count random bytes (empty when count <= 0).

fn sample_uniform(min: Float64, max: Float64) -> Float64

=== Distributions ===

fn sample_normal(mean: Float64, stddev: Float64) -> Float64

Normal (Gaussian) sample with the given mean and stddev.

fn sample_exponential(lambda: Float64) -> Float64

Exponential sample with the given rate lambda.

fn sample_bernoulli(p: Float64) -> Bool

Bernoulli trial: true with probability p.

fn sample_binomial(n: Int, p: Float64) -> Int

Binomial sample: successes in n independent trials with probability p.

fn sample_poisson(lambda: Float64) -> Int

Poisson sample with mean lambda.

fn sample_gamma(shape: Float64, scale: Float64) -> Float64

Gamma sample with the given shape and scale.

fn sample_beta(alpha: Float64, beta: Float64) -> Float64

Beta sample with parameters alpha and beta.

fn shuffle[T](items: &mut Vec[T])

=== Shuffle & Pick ===

  • Postcondition: items.len() == items.len()@pre

fn pick[T](items: &Vec[T]) -> Option[&T]

Uniformly pick one element, or None for an empty vector.

fn pick_n[T](items: &Vec[T], n: Int) -> Vec[&T]

Uniformly pick n distinct elements (fewer when the vector is shorter).

fn weighted_pick[T](items: &Vec[T], weights: &Vec[Float64]) -> Option[&T]

Pick an element with probability proportional to its weight; None when the vector is empty or all weights are <= 0.

fn uuid_v4() -> Str

Random (version 4) UUID string.

  • Postcondition: result.len() == 36

fn uuid_v7() -> Str

Time-ordered (version 7) UUID string.

  • Postcondition: result.len() == 36

fn seed_from_entropy()

=== Seeding ===

  • Precondition: true

fn seed_from_time()

Reseed the global RNG from the current time.

  • Precondition: true

fn seed_from_value(seed: Int)

Reseed the global RNG from an explicit value.

type Xorshift64

Xorshift64 PRNG (Marsaglia, 2003). State: 64-bit unsigned. Period: 2^64 - 1. Triple-xorshift: x ^= x << a; x ^= x >> b; x ^= x << c. Complexity: O(1) per call.

Field Type
state Int

Derives: Clone

fn new(seed: Int) -> Xorshift64

Creates a new Xorshift64 generator with the given seed. Seed must be non-zero. Zero seed is replaced with 1.

fn next_int(self: Self) -> Int

Returns the next pseudo-random integer from this Xorshift64 generator. Uses triple-xorshift: x ^= x << 13; x ^= x >> 7; x ^= x << 17.

fn random_choice[T](items: &Vec[T]) -> Option[&T]

Returns a random element from a vector. Returns None if the vector is empty. Wraps the existing pick function. Complexity: O(1).

fn random_shuffle[T](items: &mut Vec[T])

Shuffles a vector in place using Fisher-Yates. Wraps the existing shuffle function. Complexity: O(n), n = items length.

fn random_fraction() -> Float64

Alias for random(). Returns a Float64 in [0, 1).

fn gaussian_box_muller(mean: Float64, stddev: Float64) -> Float64

Generates a normally distributed random number using the Box-Muller transform. Mean and stddev parameters control the distribution center and spread. Complexity: O(1).

fn random_bytes_crypto(count: Int) -> Vec[UInt8]

Fills a buffer with cryptographically secure random bytes. Delegates to xiom.crypto.secure_random_bytes. Complexity: O(n), n = count.