Struct Random
pub struct Random { /* private fields */ }Expand description
A seeded random stream with reproducible, independently derived children.
Implementations§
§impl Random
impl Random
pub const VERSION: u32 = 1u32
pub const VERSION: u32 = 1u32
Version of the seed, fork and sampling algorithms documented by this module. Any incompatible change requires a new version; version 1 retains the original numeric streams.
pub fn fork(&self, stream: u64) -> Random
pub fn fork(&self, stream: u64) -> Random
Derive a child from this stream’s original seed and a stable numeric ID. Neither reads nor advances the current draw position.
pub fn fork_named(&self, name: &str) -> Random
pub fn fork_named(&self, name: &str) -> Random
Derive a child from a stable, case-sensitive UTF-8 name without consuming any draws. See the module’s version contract for the fixed hash algorithm.
use ferravex_engine::random::Random;
let mut root = Random::new(17);
let mut expected = root.fork_named("loot");
root.next_u64();
assert_eq!(expected.next_u64(), root.fork_named("loot").next_u64());pub fn fork_tagged(&self, name: &str, tag: u64) -> Random
pub fn fork_tagged(&self, name: &str, tag: u64) -> Random
Derive an independently replayable instance within a named subsystem.
Equivalent to self.fork_named(name).fork(tag); use stable IDs rather
than order-dependent counters if instance creation order can change.
use ferravex_engine::random::Random;
let root = Random::new(17);
let mut enemy = root.fork_tagged("enemies", 42);
let mut replay = root.fork_named("enemies").fork(42);
assert_eq!(enemy.next_u64(), replay.next_u64());pub fn below(&mut self, upper: u64) -> Option<u64>
pub fn below(&mut self, upper: u64) -> Option<u64>
Uniform integer in [0, upper). Rejection avoids modulo bias. Zero returns None.
pub fn try_range_u64(&mut self, range: Range<u64>) -> Option<u64>
pub fn try_range_u64(&mut self, range: Range<u64>) -> Option<u64>
Uniform integer in the half-open range, using rejection to avoid bias.
Empty or reversed ranges return None without consuming a draw.
use ferravex_engine::random::Random;
let mut rng = Random::new(5);
assert_eq!(rng.try_range_u64(9..10), Some(9));
assert_eq!(rng.try_range_u64(9..9), None);pub fn try_range_i64(&mut self, range: Range<i64>) -> Option<i64>
pub fn try_range_i64(&mut self, range: Range<i64>) -> Option<i64>
Uniform signed integer in the half-open range, including ranges that
cross zero or whose width exceeds i64::MAX. Empty or reversed ranges
return None without consuming a draw.
use ferravex_engine::random::Random;
let mut rng = Random::new(5);
let value = rng.try_range_i64(i64::MIN..i64::MAX).unwrap();
assert!((i64::MIN..i64::MAX).contains(&value));pub fn try_range_u64_inclusive(
&mut self,
range: RangeInclusive<u64>,
) -> Option<u64>
pub fn try_range_u64_inclusive( &mut self, range: RangeInclusive<u64>, ) -> Option<u64>
Uniform integer including both bounds, using rejection to avoid bias.
Reversed or exhausted ranges return None without consuming a draw.
A partially iterated range samples only its remaining values.
A singleton consumes exactly one draw. The full-width 0..=u64::MAX
returns one unmodified Self::next_u64 draw; its 2^64 values are not
treated as a zero-width range. Every other range uses Self::below for
its width, preserving the output and draw count of the equivalent
half-open Self::try_range_u64 range when that range is representable.
use ferravex_engine::random::Random;
let mut rng = Random::new(5);
assert_eq!(rng.try_range_u64_inclusive(u64::MAX..=u64::MAX), Some(u64::MAX));
let mut exhausted = 1..=1;
assert_eq!(exhausted.next(), Some(1));
let mut expected = rng.clone();
assert_eq!(rng.try_range_u64_inclusive(exhausted), None);
assert_eq!(rng.try_range_u64_inclusive(0..=u64::MAX), Some(expected.next_u64()));
assert_eq!(rng.next_u64(), expected.next_u64());pub fn try_range_i64_inclusive(
&mut self,
range: RangeInclusive<i64>,
) -> Option<i64>
pub fn try_range_i64_inclusive( &mut self, range: RangeInclusive<i64>, ) -> Option<i64>
Uniform signed integer including both bounds, using rejection to avoid
bias even when crossing zero or spanning more than i64::MAX values.
Reversed or exhausted ranges return None without consuming a draw.
A partially iterated range samples only its remaining values.
A singleton consumes exactly one draw. The full-width
i64::MIN..=i64::MAX adds one raw Self::next_u64 draw to i64::MIN
as an unsigned offset (equivalently, flip the draw’s high bit and cast to
i64). Other widths retain the output and draw count of the equivalent
half-open Self::try_range_i64 range when it is representable.
use ferravex_engine::random::Random;
let mut rng = Random::new(5);
assert_eq!(rng.try_range_i64_inclusive(i64::MAX..=i64::MAX), Some(i64::MAX));
let offset = rng.try_range_i64_inclusive(-3..=3).unwrap();
assert!((-3..=3).contains(&offset));
let mut expected = rng.clone();
assert_eq!(
rng.try_range_i64_inclusive(i64::MIN..=i64::MAX),
Some((expected.next_u64() ^ (1 << 63)) as i64),
);
assert_eq!(rng.next_u64(), expected.next_u64());pub fn try_range_f32(&mut self, range: Range<f32>) -> Option<f32>
pub fn try_range_f32(&mut self, range: Range<f32>) -> Option<f32>
Scale one 24-bit unit sample into a finite, half-open float range.
Empty, reversed or non-finite bounds return None without consuming a
draw. Uses f64 intermediates to support even -f32::MAX..f32::MAX, and
clamps rounding at the exclusive upper bound to its preceding float.
This is a scaled unit distribution, not uniform over representable floats.
use ferravex_engine::random::Random;
let mut rng = Random::new(5);
let speed = rng.try_range_f32(0.5..2.0).unwrap();
assert!((0.5..2.0).contains(&speed));
assert_eq!(rng.try_range_f32(0.0..f32::INFINITY), None);