Struct Random

pub struct Random { /* private fields */ }
Expand description

A seeded random stream with reproducible, independently derived children.

Implementations§

§

impl Random

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 new(seed: u64) -> Random

Start a stream from any 64-bit seed, including zero.

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

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

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 next_u64(&mut self) -> u64

Advance the stream and return the next SplitMix64 output.

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>

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>

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>

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>

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>

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);

pub fn unit_f32(&mut self) -> f32

Exactly 24 random mantissa bits in [0,1); independent of platform libm.

Trait Implementations§

§

impl Clone for Random

§

fn clone(&self) -> Random

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for Random

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Random

§

impl RefUnwindSafe for Random

§

impl Send for Random

§

impl Sync for Random

§

impl Unpin for Random

§

impl UnwindSafe for Random

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.