distribution
import std::random::distribution; · source
Types
Distribution<T>
type trait Distribution<T> {
sample<R>(&this, rng: mut &R) -> T
where R: RandomSource;
}
A Distribution<T> turns a source of random bits into draws of a particular shape. sample(rng) is generic over R: RandomSource, so every distribution works with Rng, SecureRng, or a custom source.
| Distribution | Produces |
|---|---|
UniformU64::new(lo, hi) | u64 in [lo, hi) |
UniformI64::new(lo, hi) | i64 in [lo, hi) |
UniformF64::new(lo, hi) | f64 in [lo, hi) |
Bernoulli::new(p) | boolean — true with probability p, clamped to [0, 1] |
Normal::new(mean, std_dev) | f64 |
Exponential::new(lambda) | f64 — waiting times at rate lambda, mean 1 / lambda |
WeightedIndex::try_new(weights) | u64 — an index chosen in proportion to its weight |
import std::random::distribution;
const d: Normal = Normal::new(0.0, 1.0);
const z: f64 = d.sample(&r);
Methods
sample<R>(&this, rng: mut &R) -> T where R: RandomSource;
sample<R>(&this, rng: mut &R) -> T where R: RandomSource;Draw one value, advancing rng.
The distributions
UniformU64, UniformI64, and UniformF64
type struct UniformU64 {
lo: u64;
hi: u64;
static new(lo: u64, hi: u64) -> UniformU64;
}
type struct UniformI64 {
lo: i64;
hi: i64;
static new(lo: i64, hi: i64) -> UniformI64;
}
type struct UniformF64 {
lo: f64;
hi: f64;
static new(lo: f64, hi: f64) -> UniformF64;
}
A value in [lo, hi), uniformly. The integer forms use the same unbiased rejection sampling as next_below; the float form scales a 53-bit draw.
UniformU64
Properties
| Property | Type |
|---|---|
lo | u64 |
hi | u64 |
Constructors
####### static new(lo: u64, hi: u64) -> UniformU64;
UniformI64
Properties
| Property | Type |
|---|---|
lo | i64 |
hi | i64 |
Constructors
####### static new(lo: i64, hi: i64) -> UniformI64;
UniformF64
Properties
| Property | Type |
|---|---|
lo | f64 |
hi | f64 |
Constructors
####### static new(lo: f64, hi: f64) -> UniformF64;
Bernoulli
type struct Bernoulli {
p: f64;
static new(p: f64) -> Bernoulli;
}
true with probability p, clamped to [0, 1].
Properties
| Property | Type |
|---|---|
p | f64 |
Constructors
static new(p: f64) -> Bernoulli;
Normal and Exponential
type struct Normal {
mean: f64;
std_dev: f64;
static new(mean: f64, std_dev: f64) -> Normal;
}
type struct Exponential {
lambda: f64;
static new(lambda: f64) -> Exponential;
}
Normal is a Box–Muller draw around mean with std_dev; Exponential gives waiting times at rate lambda, mean 1 / lambda.
Normal
Properties
| Property | Type |
|---|---|
mean | f64 |
std_dev | f64 |
Constructors
####### static new(mean: f64, std_dev: f64) -> Normal;
Exponential
Properties
| Property | Type |
|---|---|
lambda | f64 |
Constructors
####### static new(lambda: f64) -> Exponential;
WeightedIndex
type struct WeightedIndex {
cumulative: Array<f64>;
total: f64;
static try_new(weights: Slice<f64>) -> Result<WeightedIndex, RandomError>;
length(&this) -> u64;
}
WeightedIndex is built once from a weight list and then sampled cheaply. It owns its cumulative table, so it carries a drop, and construction is fallible: InvalidWeights when the list is empty, contains a negative entry, or sums to zero.
Properties
| Property | Type |
|---|---|
cumulative | Array<f64> |
total | f64 |
Constructors
static try_new(weights: Slice<f64>) -> Result<WeightedIndex, RandomError>;
static try_new(weights: Slice<f64>) -> Result<WeightedIndex, RandomError>;Build from weights. Fails with InvalidWeights if weights is empty, contains a negative entry, or sums to zero.
Methods
length(&this) -> u64;
length(&this) -> u64;The number of weights this index spans.
Trait implementations
UniformU64
implement trait Distribution<u64> for struct UniformU64
UniformI64
implement trait Distribution<i64> for struct UniformI64
UniformF64
implement trait Distribution<f64> for struct UniformF64
Bernoulli
implement trait Distribution<boolean> for struct Bernoulli
Normal
implement trait Distribution<f64> for struct Normal
Exponential
implement trait Distribution<f64> for struct Exponential
WeightedIndex
implement trait Distribution<u64> for struct WeightedIndex
implement trait Drop for struct WeightedIndex
Constants
const TWO_PI: f64 = 6.283185307179586;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 Distribution<T> {
sample<R>(&this, rng: mut &R) -> T
where R: RandomSource;
}A sampleable distribution producing values of type T.
type parameter TDistribution::sample<R>(&this, rng: mut &R) -> T
where R: RandomSourceDraw one value, advancing rng.
type parameter Rwhere R: RandomSource
&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.
(parameter) rng: mut &Rmut &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.
type parameter Twhere T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
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*>;
}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).
namespace std::random::distribution;std::random::distribution: samplers over any RandomSource.
const 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 d: Normaltype struct Normal {
mean: f64;
std_dev: f64;
}The normal distribution, parameterized by mean and std_dev.
static Normal::new(mean: f64, std_dev: f64) -> Normalconst z: f64const d: NormalNormal::sample<R>(&this, rng: mut &R) -> f64
where R: RandomSourcevia trait Distribution
std::random::distributiontype 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 UniformU64 {
lo: u64;
hi: u64;
}Uniform unsigned integers in [lo, hi).
UniformU64.lo: u64UniformU64.hi: 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 UniformU64::new(lo: u64, hi: u64) -> UniformU64(parameter) lo: u64(parameter) hi: u64type struct UniformU64 {
lo: u64;
hi: u64;
}Uniform unsigned integers in [lo, hi).
type struct UniformI64 {
lo: i64;
hi: i64;
}Uniform signed integers in [lo, hi).
UniformI64.lo: i64UniformI64.hi: i64static UniformI64::new(lo: i64, hi: i64) -> UniformI64(parameter) lo: i64(parameter) hi: i64type struct UniformI64 {
lo: i64;
hi: i64;
}Uniform signed integers in [lo, hi).
type struct UniformF64 {
lo: f64;
hi: f64;
}Uniform reals in [lo, hi).
UniformF64.lo: f64UniformF64.hi: f64static UniformF64::new(lo: f64, hi: f64) -> UniformF64(parameter) lo: f64(parameter) hi: f64type struct Bernoulli {
p: f64;
}A biased coin: true with probability p, clamped to [0, 1].
Bernoulli.p: f64static Bernoulli::new(p: f64) -> Bernoulli(parameter) p: f64type struct Bernoulli {
p: f64;
}A biased coin: true with probability p, clamped to [0, 1].
type struct Normal {
mean: f64;
std_dev: f64;
}The normal distribution, parameterized by mean and std_dev.
Normal.mean: f64Normal.std_dev: f64static Normal::new(mean: f64, std_dev: f64) -> Normal(parameter) mean: f64(parameter) std_dev: f64type struct Exponential {
lambda: f64;
}The exponential distribution at rate lambda (mean 1 / lambda).
Exponential.lambda: f64static Exponential::new(lambda: f64) -> Exponential(parameter) lambda: f64type struct Exponential {
lambda: f64;
}The exponential distribution at rate lambda (mean 1 / lambda).
type struct WeightedIndex {
cumulative: Array<f64>;
total: f64;
}Chooses an index 0..n with probability proportional to a weight. Built once from a weight list, then sampled cheaply. Owns its cumulative table, so it carries a drop.
WeightedIndex.cumulative: Array<f64>type struct Array<T, A = GlobalAlloc> where A: Allocator {
ptr: T*;
length: i64;
capacity: i64;
alloc: A;
}Array<T, A>: growable, heap-backed, contiguous sequence.
std::collections::arrayWeightedIndex.total: f64static WeightedIndex::try_new(weights: Slice<f64>) -> Result<WeightedIndex, RandomError>Build from weights. Fails with InvalidWeights if weights is empty, contains a negative entry, or sums to zero.
(parameter) weights: Slice<f64>type struct Slice<T> {
ptr: T*;
length: u64;
}Slice<T>: a borrowed view into a contiguous sequence.
std::core::slicetype struct WeightedIndex {
cumulative: Array<f64>;
total: f64;
}Chooses an index 0..n with probability proportional to a weight. Built once from a weight list, then sampled cheaply. Owns its cumulative table, so it carries a drop.
type enum RandomError {
EntropyUnavailable;
EmptyRange;
InvalidWeights;
}What went wrong in a fallible random operation.
std::random::errorWeightedIndex::length(&this) -> u64The number of weights this index spans.
const TWO_PI: f64 = 6.283185307179586;type trait Distribution<T> {
sample<R>(&this, rng: mut &R) -> T
where R: RandomSource;
}A sampleable distribution producing values of type T.
Distribution::sample<R>(&this, rng: mut &R) -> T
where R: RandomSourceDraw one value, advancing rng.
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.
static UniformU64::new(lo: u64, hi: u64) -> UniformU64UniformU64.lo: u64UniformU64.hi: u64static UniformI64::new(lo: i64, hi: i64) -> UniformI64static UniformF64::new(lo: f64, hi: f64) -> UniformF64static Bernoulli::new(p: f64) -> BernoulliBernoulli.p: f64Normal.mean: f64Normal.std_dev: f64static Exponential::new(lambda: f64) -> ExponentialExponential.lambda: f64static WeightedIndex::try_new(weights: Slice<f64>) -> Result<WeightedIndex, RandomError>Build from weights. Fails with InvalidWeights if weights is empty, contains a negative entry, or sums to zero.
trueThe boolean literal. Booleans are a distinct 1-byte type, not interchangeable with integers; if and while conditions must be boolean.
RandomError::InvalidWeightsWeightedIndex.cumulative: Array<f64>WeightedIndex.total: f64implement 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