source
import std::random::source; · source
Types
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.
Methods
next_64(mut &this) -> u64;
next_64(mut &this) -> u64;The single required primitive: the next 64 random bits.
next<T>(mut &this) -> T;
next<T>(mut &this) -> T;A uniform draw of type T, dispatched at compile time via static match (T) over the primitive next_64. Covers every fixed-width integer, the platform-width usize/isize, boolean, f32/f64, and char: - integers - the required number of bits, taken from the top (best-mixed) end for the sub-64-bit widths; u128/i128 concatenate two draws. - boolean - the top (highest-quality) 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 value (the surrogate ra
next_below(mut &this, bound: u64) -> u64;
next_below(mut &this, bound: u64) -> u64;A uniform integer in [0, bound). Unbiased: the top partial bucket is rejected so the modulo introduces no skew. Panics when bound == 0 - an empty range has no value to return.
next_range<T>(mut &this, lo: T, hi: T) -> T;
next_range<T>(mut &this, lo: T, hi: T) -> T;A uniform integer of type T in [lo, hi). Requires lo < hi. static match (T) covers every integer width: the spans of the 64-bit-and-narrower types are drawn unbiased through next_below; u128/i128 use an inline 128-bit rejection sampler.
next_below_wide(mut &this, bound: u128) -> u128;
next_below_wide(mut &this, bound: u128) -> u128;A uniform u128 in [0, bound), unbiased by the same top-bucket rejection as next_below but over the full 128-bit range. Backs the u128/i128 arms of next_range.
fill(mut &this, dst: Slice<u8>) -> void;
fill(mut &this, dst: Slice<u8>) -> void;Fill dst with random bytes, writing through the view.
shuffle<T>(mut &this, items: &Slice<T>) -> void;
shuffle<T>(mut &this, items: &Slice<T>) -> void;Shuffle items in place with a uniform Fisher-Yates pass. Works for any element type - elements are swapped, never copied out, so no Copy bound is needed.
choose<T>(mut &this, items: &Slice<T>) -> Option<T*>;
choose<T>(mut &this, items: &Slice<T>) -> Option<T*>;Borrow a uniformly chosen element of items, or None when items is empty. Returns a pointer in place, so no Copy is required and owning element types are fine.
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
Constants
const U64_MAX: u64 = 0xFFFFFFFFFFFFFFFF;type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
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*>;
}RandomSource::next_64(mut &this) -> u64The single required primitive: the next 64 random bits.
mut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
RandomSource::next<T>(mut &this) -> TA uniform draw of type T, dispatched at compile time via static match (T) over the primitive next_64.
type parameter Ttype parameter TRandomSource::next_below(mut &this, bound: u64) -> u64A uniform integer in [0, bound). Unbiased: the top partial bucket is rejected so the modulo introduces no skew. Panics when bound == 0 - an empty range has no value to return.
(parameter) bound: u64RandomSource::next_range<T>(mut &this, lo: T, hi: T) -> TA uniform integer of type T in [lo, hi). Requires lo < hi. static match (T) covers every integer width: the spans of the 64-bit-and-narrower types are drawn unbiased through next_below; u128/i128 use an inline 128-bit rejection sampler.
(parameter) lo: Ttype parameter T(parameter) hi: TRandomSource::next_below_wide(mut &this, bound: u128) -> u128A uniform u128 in [0, bound), unbiased by the same top-bucket rejection as next_below but over the full 128-bit range. Backs the u128/i128 arms of next_range.
(parameter) bound: u128RandomSource::fill(mut &this, dst: Slice<u8>) -> voidFill dst with random bytes, writing through the view.
(parameter) dst: Slice<u8>type struct Slice<T> {
ptr: T*;
length: u64;
}Slice<T>: a borrowed view into a contiguous sequence.
std::core::sliceRandomSource::shuffle<T>(mut &this, items: &Slice<T>) -> voidShuffle items in place with a uniform Fisher-Yates pass. Works for any element type - elements are swapped, never copied out, so no Copy bound is needed.
(parameter) items: &Slice<T>type parameter TRandomSource::choose<T>(mut &this, items: &Slice<T>) -> Option<T*>Borrow a uniformly chosen element of items, or None when items is empty. Returns a pointer in place, so no Copy is required and owning element types are fine.
type parameter Tconst NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const U64_MAX: u64 = 0xFFFFFFFFFFFFFFFF;import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
RandomSource::next_64(mut &this) -> u64The single required primitive: the next 64 random bits.
std::random::sourceRandomSource::next<T>(mut &this) -> TA uniform draw of type T, dispatched at compile time via static match (T) over the primitive next_64.
static match (T) { ... }Opens a compile-time match on a type parameter: the one arm whose type matches is kept during monomorphisation and the rest are discarded.
static match (T) {
u8 | u16 => { ... }
_ => { ... }
}Selects a body by inspecting a type parameter at compile time. The subject is a type and each arm is a type (| groups several; _ takes the rest). During monomorphisation the one matching arm is kept and the others discarded, so there is no runtime branch.
primitive usizePointer-width unsigned integer; the natural type for sizes and indices.
primitive typeRandomSource::next_below(mut &this, bound: u64) -> u64A uniform integer in [0, bound). Unbiased: the top partial bucket is rejected so the modulo introduces no skew. Panics when bound == 0 - an empty range has no value to return.
RandomSource::next_range<T>(mut &this, lo: T, hi: T) -> TA uniform integer of type T in [lo, hi). Requires lo < hi. static match (T) covers every integer width: the spans of the 64-bit-and-narrower types are drawn unbiased through next_below; u128/i128 use an inline 128-bit rejection sampler.
RandomSource::next_below_wide(mut &this, bound: u128) -> u128A uniform u128 in [0, bound), unbiased by the same top-bucket rejection as next_below but over the full 128-bit range. Backs the u128/i128 arms of next_range.
RandomSource::fill(mut &this, dst: Slice<u8>) -> voidFill dst with random bytes, writing through the view.
RandomSource::shuffle<T>(mut &this, items: &Slice<T>) -> voidShuffle items in place with a uniform Fisher-Yates pass. Works for any element type - elements are swapped, never copied out, so no Copy bound is needed.
type trait Copy {}Types whose values can be duplicated by a bitwise copy. Integers, floats, pointers, and pure-POD structs qualify; anything that owns resources (heap storage, file handles, refcount) does not.
std::core::markerRandomSource::choose<T>(mut &this, items: &Slice<T>) -> Option<T*>Borrow a uniformly chosen element of items, or None when items is empty. Returns a pointer in place, so no Copy is required and owning element types are fine.
Option::Noneimplement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referencetype 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*>;
}implement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referencetype struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Rng {
s0: u64;
s1: u64;
s2: u64;
s3: u64;
}A fast non-cryptographic PRNG (xoshiro256**). The 256-bit state is the four words s0..s3.
type struct SecureRng {}Cryptographically secure source backed by the kernel CSPRNG. Slower than Rng; use it only where unpredictability matters.