Skip to content
CryoCryo home
StdlibNumerics

random

Two generators and a set of distributions over them. Both generators implement RandomSource, so the whole draw surface works with either — or with a source of your own.

public module random::error;
public module random::bits;
public module random::source;
public module random::rng;
public module random::secure;
public module random::distribution;
FileWhat it holdsImport
sourceRandomSource, the trait both generators implementimport std::random::source;
rngRng, xoshiro256**import std::random::rng;
secureSecureRng, the kernel CSPRNGimport std::random::secure;
distributionDistribution, UniformU64, Normal, WeightedIndex, ...import std::random::distribution;
errorRandomErrorimport std::random::error;

random::bits holds the two little-endian load/store helpers the generators share; there is nothing in it for user code.

Choosing a generator

Rng is xoshiro256** — excellent statistical quality, 256 bits of state, very fast. It is not suitable for cryptography.

SecureRng draws straight from the operating system's cryptographic generator. It exposes no predictable algorithm and is the right choice for keys, tokens, and nonces.

import std::random::rng;

mut r: Rng = Rng::from_seed(42);        // reproducible
const roll: u64 = r.next_range(1, 7);