Skip to content
volt
GitHub

std::random

Pseudo-random numbers (xoshiro256**), seeded or from the operating system, and the OS's own secure random bytes.

Source: std/random.volt

Types

struct std::random::rng

struct rng

xoshiro256** (Blackman and Vigna): fast, with good statistical quality, and the same numbers for the same seed. Not for secrets (keys, tokens): use os_bytes for those

Field
s0: u64the 256-bit state
s1: u64
s2: u64
s3: u64

Methods

attach fn next_u64(this: std::random::rng&) -> u64

the next 64 random bits

attach fn below(this: std::random::rng&, n: u64) -> u64

a number in 0..n (n > 0), every one equally likely (Lemire's multiply-shift: no modulo bias)

attach fn range(this: std::random::rng&, lo: i64, hi: i64) -> i64

an integer in lo..hi (hi excluded; lo < hi), every one equally likely

attach fn float(this: std::random::rng&) -> f64

a float in [0, 1), from the top 53 bits

attach fn chance(this: std::random::rng&, p: f64) -> bool

true with probability p (0 never, 1 always)

<T: type> attach fn shuffle(this: std::random::rng&, xs: T[..]) -> void

put xs in a random order, every order equally likely (Fisher-Yates)

<T: type> attach fn choose(this: std::random::rng&, xs: T[..]) -> T*

a random element of xs, or null when it's empty

Functions

std::random::seeded

fn seeded(seed: u64) -> rng

a generator that gives the same numbers every time for the same seed

std::random::os_bytes

fn os_bytes(buf: u8[..]) -> bool

fill buf from the operating system's secure random source; false if it can't

std::random::os_seeded

fn os_seeded() -> rng

a generator seeded from the operating system: different numbers every run