Stdlibrandom
source
import std::random::source; · source
RandomSource
type trait RandomSource {
next_64(mut &this) -> u64;
next<T>(mut &this) -> T;
next_below(mut &this, bound: u64) -> u64;
next_range<T>(mut &this, lo: T, hi: T) -> T;
next_below_wide(mut &this, bound: u128) -> u128;
fill(mut &this, dst: Slice<u8>) -> void;
shuffle<T>(mut &this, items: &Slice<T>) -> void;
choose<T>(mut &this, items: &Slice<T>) -> Option<T*>;
}
One required primitive — next_64 — and a substantial set of defaults built on it.
next<T>()
A uniform draw of type T, dispatched at compile time via static match (T). Select the type with a turbofish (r.next<u32>()) or let context infer it.
T | What you get |
|---|---|
Any fixed-width integer, usize, isize | The required bits, taken from the top (best-mixed) end. u128 and i128 concatenate two draws. |
boolean | The top bit. |
f64 / f32 | A uniform value in [0.0, 1.0). f64 uses the top 53 bits, so every representable value in the interval is equally likely. |
char | A uniform Unicode scalar; the surrogate range is skipped. |
Bounded draws
next_below(bound)— uniform in[0, bound). Unbiased: the top partial bucket is rejected so the modulo introduces no skew. Panics whenbound == 0, since an empty range has no value to return.next_range<T>(lo, hi)— uniform in[lo, hi), requiringlo < hi. Every integer width is covered; 64-bit-and-narrower spans go throughnext_below, andu128/i128use an inline 128-bit rejection sampler.next_below_wide(bound)— the same top-bucket rejection over the full 128-bit range.
Bulk and collections
fill(dst)— fill a byte view.shuffle<T>(items)— an in-place uniform Fisher-Yates pass. Elements are swapped, never copied out, so there is noCopybound and owning element types work.choose<T>(items)— a uniformly chosen element, borrowed in place.Nonewhen empty.
Implementors
implement trait RandomSource for struct Rng // std::random::rng
implement trait RandomSource for struct SecureRng // std::random::secure