pub struct Random { /* private fields */ }Expand description
A reproducible local random stream derived entirely from a hashed key.
Key bytes use FNV-1a, followed by SplitMix64 for each sample. Integer hashing uses little-endian bytes and widens usize/isize to 64 bits. Custom Hash implementations must themselves be deterministic. Rust’s Hash encodings are not a serialization format; cross-version key encoding stability is not promised. This generator is not cryptographic and has no shared mutable state.
use motion_core::Random;
let mut first = Random::from_key(("spark", 42_u64));
let mut second = Random::from_key(("spark", 42_u64));
assert_eq!(first.next_float(), second.next_float());
assert!((10.0..20.0).contains(&first.range(10.0..20.0)));
assert_eq!(first.choose(&[7]), Some(&7));Implementations§
Source§impl Random
impl Random
Sourcepub fn from_key(key: impl Hash) -> Self
pub fn from_key(key: impl Hash) -> Self
Starts a fresh stream; equal keys reproduce the same sequence.
Sourcepub fn for_particle(seed: u64, index: u64, step: u64) -> Self
pub fn for_particle(seed: u64, index: u64, step: u64) -> Self
Starts a counter-keyed particle stream without Hash encoding or shared state. Equal seed/index/step values are independent of traversal and worker count.
Sourcepub fn next_float(&mut self) -> f64
pub fn next_float(&mut self) -> f64
Returns one of 2^53 equally spaced floating-point values in [0, 1).
Sourcepub fn range(&mut self, range: Range<f64>) -> f64
pub fn range(&mut self, range: Range<f64>) -> f64
Samples a finite half-open interval, including adjacent float endpoints.
Rounding is clamped below the upper endpoint; no overflowing endpoint subtraction is performed.
§Panics
Panics for an empty, reversed or nonfinite range; see Self::try_range.
Sourcepub fn try_range(&mut self, range: Range<f64>) -> Result<f64, ValidationError>
pub fn try_range(&mut self, range: Range<f64>) -> Result<f64, ValidationError>
Samples like Self::range, reporting an invalid range without
advancing the stream.
Sourcepub fn unit_vector(&mut self) -> Vector2
pub fn unit_vector(&mut self) -> Vector2
Samples a uniform angle using deterministic libm trigonometry.