rng
import std::random::rng; · source
Types
Rng
type struct Rng {
s0: u64;
s1: u64;
s2: u64;
s3: u64;
static from_seed(seed: u64) -> Rng;
static from_os() -> Rng;
}
from_seed(seed) gives the same stream for the same seed, which is what you want for tests and simulations; the 64-bit seed is expanded to the 256-bit state with SplitMix64. from_os() seeds unpredictably, 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.
Rng is POD, so copying it forks the stream rather than sharing it.
Properties
| Property | Type |
|---|---|
s0 | u64 |
s1 | u64 |
s2 | u64 |
s3 | u64 |
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.
static from_os() -> Rng;
static from_os() -> Rng;Seed from the OS CSPRNG. Falls back to a monotonic-clock + pid mix if the kernel source is briefly unavailable, so it never hard-fails. (The fallback is fine for non-crypto use; for cryptography use SecureRng.) Routes through secure::fill_secure_bytes so the OS source is portable - getrandom on UNIX, RtlGenRandom on Windows - instead of binding the Linux-only getrandom symbol directly.
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 {
s0: u64;
s1: u64;
s2: u64;
s3: u64;
}A fast non-cryptographic PRNG (xoshiro256**). The 256-bit state is the four words s0..s3.
Rng.s0: u64Rng.s1: u64Rng.s2: u64Rng.s3: u64static 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.
static Rng::from_seed(seed: u64) -> RngSeed deterministically from a single 64-bit value. The same seed always produces the same sequence.
(parameter) seed: u64type 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.
static Rng::from_os() -> RngSeed from the OS CSPRNG. Falls back to a monotonic-clock + pid mix if the kernel source is briefly unavailable, so it never hard-fails.
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).
static Rng::from_seed(seed: u64) -> RngSeed deterministically from a single 64-bit value. The same seed always produces the same sequence.
std::random::rngstatic Rng::from_os() -> RngSeed from the OS CSPRNG. Falls back to a monotonic-clock + pid mix if the kernel source is briefly unavailable, so it never hard-fails.
std::random::rngRng.s0: u64Rng.s1: u64Rng.s2: u64Rng.s3: u64type struct SecureRng {}Cryptographically secure source backed by the kernel CSPRNG. Slower than Rng; use it only where unpredictability matters.
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 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 reference