docs

std::random

本页内容

API 参考只有英文版,由标准库源代码中的英文文档注释生成。

cryptographically secure random bytes from the OS (getrandom on Linux, getentropy on macOS, BCryptGenRandom on Windows, random_get on WASI), seedable generators (Rng) with ranges of any integer type and the common distributions, and one-call forms over a shared generator. A failing OS source is not recoverable: it panics. The algorithms that use the bytes are core::crypto.

Structs

Xoshiro256PlusPlusSource

pub struct Xoshiro256PlusPlus

Xoshiro256++ (Blackman and Vigna; Julia's default): 256 bits of state, period 2^256 - 1. Fast and statistically strong, not for secrets (use the OS source: random::bytes, OsRng).

  • pub const fn seed(seed: u64) -> Xoshiro256PlusPlus§

    Source

    The generator for seed: its state is four outputs of SplitMix64 started at seed. The same seed gives the same stream on every target and release.

  • pub fn from_os() -> Xoshiro256PlusPlus§

    Source

    A generator seeded from the OS source: a different stream each time.

  • pub fn split(ref self) -> Xoshiro256PlusPlus§

    Source

    A generator for another stream, for a parallel task: a copy of this one, which then jumps 2^128 steps ahead (Vigna's jump), so the two never overlap in practice. The streams depend only on the seed and the order of the calls, not on threads or timing.

Trait implementations 1
  • impl Rng for Xoshiro256PlusPlus

OsRngSource

pub struct OsRng

The OS source as an Rng: every value read from it (a system call each), never reproducible.

Trait implementations 1
  • impl Rng for OsRng

Traits

RngSource

pub trait Rng

A source of random u64s and the values derived from them.

  • fn next_u64(ref self) -> u64§

    Source

    The next 64 random bits.

  • const fn f64(ref self) -> f64§

    Source

    A value in [0, 1): the top 53 bits of next_u64 times 2^-53. Python: random.random().

  • const fn below(ref self, n: u64) -> u64§

    Source

    A value in [0, n), every one equally likely (bits below the last whole multiple of n are drawn again). Panics when n is 0.

  • const fn f32(ref self) -> f32§

    Source

    A value in [0, 1): the top 24 bits of next_u64 times 2^-24.

  • const fn range<R: SampleRange>(ref self, r: R) -> R::Item§

    Source

    A value of r, every one equally likely: a range of any integer type (0..6, 1..=6, 0u8..=255, 0usize..n). Panics when r is empty. Python: random.randrange(a, b) / random.randint(a, b); for floats, uniform.

    nabi
    use std::random::{Rng, Xoshiro256PlusPlus};
    
    let g = Xoshiro256PlusPlus::seed(1);
    let die = g.range(1..=6);
    let i = g.range(0usize..10);
    let x = g.uniform(-1.0, 1.0);
    assert(die >= 1 && die <= 6 && i < 10 && x >= -1.0 && x < 1.0);
    
  • const fn uniform(ref self, lo: f64, hi: f64) -> f64§

    Source

    A value in [lo, hi): lo + (hi - lo) * self.f64(). Python: random.uniform(lo, hi).

  • const fn bool(ref self, p: f64 = 0.5) -> bool§

    Source

    true with probability p (self.f64() < p).

  • const fn normal(ref self, mean: f64 = 0.0, std: f64 = 1.0) -> f64§

    Source

    A normal deviate with mean mean and standard deviation std (the ziggurat of Marsaglia and Tsang: one 64-bit draw nearly always). Python: random.gauss(mean, std).

  • const fn exponential(ref self, rate: f64 = 1.0) -> f64§

    Source

    An exponential deviate with rate rate (mean 1 / rate): -ln(U) / rate. Python: random.expovariate(rate).

  • const fn gamma(ref self, shape: f64, scale: f64 = 1.0) -> f64§

    Source

    A gamma deviate of shape shape and scale scale (mean shape * scale). Python: random.gammavariate(shape, scale).

  • const fn beta(ref self, a: f64, b: f64) -> f64§

    Source

    A beta deviate in [0, 1] with shapes a and b. Python: random.betavariate(a, b).

  • const fn poisson(ref self, lam: f64) -> i64§

    Source

    A Poisson count with mean lam (lam >= 0). NumPy: rng.poisson(lam).

  • const fn binomial(ref self, n: i64, p: f64) -> i64§

    Source

    The number of successes in n trials of probability p. NumPy: rng.binomial(n, p).

    nabi
    use std::random::{Rng, Xoshiro256PlusPlus};
    
    let g = Xoshiro256PlusPlus::seed(7);
    let k = g.binomial(10, 0.5);
    assert(k >= 0 && k <= 10 && g.poisson(3.0) >= 0);
    assert(g.gamma(2.0) > 0.0 && g.exponential() > 0.0);
    let b = g.beta(2.0, 5.0);
    assert(b >= 0.0 && b <= 1.0);
    
  • const fn weighted(ref self, weights: List<f64>) -> usize§

    Source

    An index of weights, i with probability weights[i] / sum. Panics for a negative or non-finite weight, or when all are 0. Python: random.choices(range(n), weights)[0].

    nabi
    use std::random::{Rng, Xoshiro256PlusPlus};
    
    let g = Xoshiro256PlusPlus::seed(3);
    let i = g.weighted([0.0, 1.0, 0.0]);
    assert(i == 1);
    
  • const fn fill_f64(ref self, out: SliceMut<f64>)§

    Source

    Fill out with values in [0, 1) (f64 each).

  • const fn fill_normal(ref self, out: SliceMut<f64>, mean: f64 = 0.0, std: f64 = 1.0)§

    Source

    Fill out with normal deviates (normal each).

  • const fn random_array<const N: usize>(ref self, shape: [usize; N]) -> Array<f64, N>§

    Source

    An array of shape with values in [0, 1). NumPy: rng.random(shape).

    nabi
    use std::random::{Rng, Xoshiro256PlusPlus};
    
    let g = Xoshiro256PlusPlus::seed(5);
    let a = g.random_array([2, 3]);
    let z = g.normal_array([1000], mean: 10.0);
    assert(a.shape() == [2, 3] && (z.mean() - 10.0).abs() < 0.2);
    
  • const fn normal_array<const N: usize>(ref self, shape: [usize; N], mean: f64 = 0.0, std: f64 = 1.0) -> Array<f64, N>§

    Source

    An array of shape of normal deviates. NumPy: rng.normal(mean, std, shape).

  • const fn uniform_array<const N: usize>(ref self, shape: [usize; N], lo: f64, hi: f64) -> Array<f64, N>§

    Source

    An array of shape with values in [lo, hi). NumPy: rng.uniform(lo, hi, shape).

  • const fn shuffle<T>(ref self, xs: List<T>)§

    Source

    The items of xs in a random order, in place (Fisher-Yates from the back: position i swaps with below(i + 1)).

  • const fn choice<T>(ref self, xs: List<T>) -> T | none§

    Source

    One item of xs (xs[below(len)]), or none when it is empty. Python: random.choice(xs).

  • const fn sample<T>(ref self, xs: List<T>, k: usize) -> List<T>§

    Source

    k items of xs at distinct positions, in the order drawn (a partial Fisher-Yates over the positions). Panics when k is more than xs.len(). Python: random.sample(xs, k).

SampleRangeSource

pub trait SampleRange

A range Rng::range draws from: a..b and a..=b of every integer type. For floats, Rng::uniform(lo, hi).

  • type Item§

    Source
  • fn outcomes(self) -> u64§

    Source

    How many values the range holds (0: all 2^64); panics when it is empty.

  • fn outcome(self, k: u64) -> Item§

    Source

    The value k steps past the start.

Functions

fillSource

pub fn fill(buf: SliceMut<u8>)

Overwrite every byte of buf with random bytes. A List<u8> passes as a view of all of it.

bytesSource

pub fn bytes(n: usize) -> List<u8>

n random bytes.

secretSource

pub fn secret(n: usize) -> Secret

n random bytes in secret storage (keys, seeds): no other copy.

rngSource

pub fn rng() -> Xoshiro256PlusPlus

A generator seeded from the OS source (Xoshiro256PlusPlus::from_os). For a stream that repeats, use Xoshiro256PlusPlus::seed(n).

f64Source

pub fn f64() -> f64

A value in [0, 1) from the shared generator. Python: random.random().

nabi
use std::random;

let x = random::f64();
let n = random::range(1..=6);
assert(x >= 0.0 && x < 1.0 && n >= 1 && n <= 6);

rangeSource

pub fn range<R: SampleRange>(r: R) -> R::Item

A value of r from the shared generator (see Rng::range).

normalSource

pub fn normal(mean: f64 = 0.0, std: f64 = 1.0) -> f64

A normal deviate from the shared generator. Python: random.gauss(mean, std).

shuffleSource

pub fn shuffle<T>(xs: List<T>)

xs in a random order, in place, from the shared generator.

choiceSource

pub fn choice<T>(xs: List<T>) -> T | none

One item of xs from the shared generator, or none when it is empty.