generator
import std::random::generator; · source
Types
Rng
type struct Rng {
private s0: u64;
private s1: u64;
private s2: u64;
private s3: u64;
}
A fast non-cryptographic generator, xoshiro256**. from_seed(seed) gives the same stream for the same seed, on every version of the library, which is what you want for tests and simulations; the 64-bit seed is expanded to the 256-bit state with SplitMix64. from_operating_system() seeds unpredictably from SecureRng, falling back to a monotonic-clock and pid mix if the kernel source is briefly unavailable, so it never hard-fails — fine for non-cryptographic use.
for_current_thread() is the calling thread's own generator, seeded from the OS the first time the thread asks for it. Every thread has a separate stream, so no lock is taken and no two threads draw the same values; the free functions of std::random draw from it.
import std::random::generator::{ Rng };
import std::random::source::{ RandomSource };
mut rng: Rng = Rng::from_seed(7);
const coin: boolean = rng.next::<boolean>();
const index: i64 = rng.next_below(10); // 0 through 9
const x: f64 = rng.next_range(-1.0, 1.0); // -1.0 up to, not including, 1.0
Rng is POD, so copying it forks the stream rather than sharing it.
Constructors
static from_seed(seed: u64) -> Rng;
static from_seed(seed: u64) -> Rng;Seed deterministically from a single 64-bit value. The same seed always produces the same sequence, on every version of the library: a program that saved a seed replays its values, so the generator, the seed expansion and the output scrambler cannot change.
static from_operating_system() -> Rng;
static from_operating_system() -> Rng;Seed from the OS CSPRNG (SecureRng). Falls back to a monotonic-clock + pid mix if the kernel source is unavailable, so it never hard-fails. (The fallback is fine for non-crypto use; for cryptography use SecureRng.)
static for_current_thread() -> mut &Rng;
static for_current_thread() -> mut &Rng;The calling thread's own generator, seeded from the OS (as from_operating_system) the first time the thread asks for it. Every thread has a separate stream, so no lock is taken and no two threads draw the same values. The free functions of std::random draw from it.
Trait implementations
implement trait RandomSource for struct Rng
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 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 {}A fast non-cryptographic PRNG (xoshiro256**). The 256-bit state is the four words s0..s3.
private type struct Name { ... }
private:Confined to the module that declares it - for items and members alike. A private field or function reached from another module is E0353, and a private type named there is E0503. private: opens a block of such members.
Rng.s0: u64Rng.s1: u64Rng.s2: u64Rng.s3: u64import 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).
namespace std::random::generator;std::random::generator: Rng, a fast non-cryptographic generator.
type struct Rng {}A fast non-cryptographic PRNG (xoshiro256**). The 256-bit state is the four words s0..s3.
type trait RandomSource {
next_64(mut &this) -> u64;
next<T>(mut &this) -> T;
next_below(mut &this, bound: i64) -> i64;
next_range<T>(mut &this, lo: T, hi: T) -> T
where T: Numeric;
next_below_wide(mut &this, bound: u128) -> u128;
fill(mut &this, dst: mut &Slice<u8>) -> void;
shuffle<T>(mut &this, items: mut &Slice<T>) -> void;
choose<T>(mut &this, items: &Slice<T>) -> Option<T*>;
}A generator of random bits. An implementation supplies only next_64; every other draw (next, next_below, next_range, fill, shuffle, choose) is a default method built on it. Rng and SecureRng implement it, and the samplers in random::distribution take any R: RandomSource.
mut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut rng: Rngstatic Rng::from_seed(seed: u64) -> RngSeed deterministically from a single 64-bit value. The same seed always produces the same sequence, on every version of the library: a program that saved a seed replays its values, so the generator, the seed expansion and the output scrambler cannot change.
std::random::generatorconst 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 coin: booleanmut rng: RngRng::next<T>(mut &this) -> Tvia trait RandomSource
A uniform draw of type T, dispatched at compile time via static match (T) over the primitive next_64.
const index: i64Rng::next_below(mut &this, bound: i64) -> i64via trait RandomSource
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.
const x: f64Rng::next_range<T>(mut &this, lo: T, hi: T) -> T
where T: Numericvia trait RandomSource
A uniform value of type T in [lo, hi), panicking unless lo < hi. Every integer width is drawn unbiased: a span of a 64-bit-and-narrower type through next_below, and through the 128-bit sampler next_below_wide when it is wider than i64::MAX; u128/i128 always use the 128-bit sampler.
static Rng::from_operating_system() -> RngSeed from the OS CSPRNG (SecureRng). Falls back to a monotonic-clock + pid mix if the kernel source is unavailable, so it never hard-fails. (The fallback is fine for non-crypto use; for cryptography use SecureRng.)
type struct SecureRng {}Cryptographically secure source backed by the kernel CSPRNG. Slower than Rng; use it only where unpredictability matters.
static Rng::for_current_thread() -> mut &RngThe calling thread's own generator, seeded from the OS (as from_operating_system) the first time the thread asks for it. Every thread has a separate stream, so no lock is taken and no two threads draw the same values. The free functions of std::random draw from it.
static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
(parameter) seed: u64mut &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.
implement 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 referenceimplement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language reference