Skip to content
CryoCryo home
Stdlibrandom

generator

import std::random::generator; · source

Types

Rng

type struct Rng {
    private s0: u64;
    private s1: u64;
    private s2: u64;
    private s3: u64;
}

A fast non-cryptographic generator, xoshiro256**. from_seed(seed) gives the same stream for the same seed, on every version of the library, which is what you want for tests and simulations; the 64-bit seed is expanded to the 256-bit state with SplitMix64. from_operating_system() seeds unpredictably from SecureRng, falling back to a monotonic-clock and pid mix if the kernel source is briefly unavailable, so it never hard-fails — fine for non-cryptographic use.

for_current_thread() is the calling thread's own generator, seeded from the OS the first time the thread asks for it. Every thread has a separate stream, so no lock is taken and no two threads draw the same values; the free functions of std::random draw from it.

import std::random::generator::{ Rng };
import std::random::source::{ RandomSource };

mut rng: Rng = Rng::from_seed(7);
const coin: boolean = rng.next::<boolean>();
const index: i64 = rng.next_below(10);           // 0 through 9
const x: f64 = rng.next_range(-1.0, 1.0);        // -1.0 up to, not including, 1.0

Rng is POD, so copying it forks the stream rather than sharing it.

Constructors

static from_seed(seed: u64) -> Rng;

Seed deterministically from a single 64-bit value. The same seed always produces the same sequence, on every version of the library: a program that saved a seed replays its values, so the generator, the seed expansion and the output scrambler cannot change.

static from_operating_system() -> Rng;

Seed from the OS CSPRNG (SecureRng). Falls back to a monotonic-clock + pid mix if the kernel source is unavailable, so it never hard-fails. (The fallback is fine for non-crypto use; for cryptography use SecureRng.)

static for_current_thread() -> mut &Rng;

The calling thread's own generator, seeded from the OS (as from_operating_system) the first time the thread asks for it. Every thread has a separate stream, so no lock is taken and no two threads draw the same values. The free functions of std::random draw from it.

Trait implementations

implement trait RandomSource for struct Rng