pub struct SimulationRng { /* private fields */ }Expand description
A thread-safe, seeded random number generator for simulation.
All random decisions during simulation should go through this RNG to ensure deterministic replay when using the same seed.
§Determinism
For deterministic replay:
- All random decisions must use this RNG
- Random calls must happen in the same order each run
- Parallel access must be serialized (handled internally by Mutex)
Implementations§
Source§impl SimulationRng
impl SimulationRng
Sourcepub fn gen_bool(&self, probability: f64) -> bool
pub fn gen_bool(&self, probability: f64) -> bool
Generates a random boolean with the given probability of being true.
Sourcepub fn gen_range(&self, range: Range<usize>) -> usize
pub fn gen_range(&self, range: Range<usize>) -> usize
Generates a random usize in the given range.
Sourcepub fn gen_range_u64(&self, range: Range<u64>) -> u64
pub fn gen_range_u64(&self, range: Range<u64>) -> u64
Generates a random u64 in the given range.
Sourcepub fn gen_duration(&self, range: Range<Duration>) -> Duration
pub fn gen_duration(&self, range: Range<Duration>) -> Duration
Generates a random Duration within the given range.
Sourcepub fn choose<'a, T>(&self, slice: &'a [T]) -> Option<&'a T>
pub fn choose<'a, T>(&self, slice: &'a [T]) -> Option<&'a T>
Chooses a random element from a slice, returning None if empty.
Sourcepub fn shuffle<T>(&self, slice: &mut [T])
pub fn shuffle<T>(&self, slice: &mut [T])
Shuffles a slice in-place using Fisher-Yates algorithm.
Sourcepub fn child(&self) -> Self
pub fn child(&self) -> Self
Creates a child RNG with a derived seed.
This is useful for giving each peer its own RNG while maintaining overall determinism - the child’s seed is derived from the parent’s current state.
Sourcepub fn child_with_index(&self, index: u64) -> Self
pub fn child_with_index(&self, index: u64) -> Self
Creates a child RNG with a specific derived seed based on an index.
This ensures that child RNGs are created deterministically regardless of the order in which they’re requested, as long as the indices are consistent.
Sourcepub fn lock(&self) -> MutexGuard<'_, SmallRng>
pub fn lock(&self) -> MutexGuard<'_, SmallRng>
Access the inner RNG for operations that need direct Rng trait access.
§Warning
The returned guard holds the lock. Be careful not to hold it across await points or other blocking operations.
Trait Implementations§
Source§impl Clone for SimulationRng
impl Clone for SimulationRng
Source§fn clone(&self) -> SimulationRng
fn clone(&self) -> SimulationRng
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for SimulationRng
impl RefUnwindSafe for SimulationRng
impl Send for SimulationRng
impl Sync for SimulationRng
impl Unpin for SimulationRng
impl UnsafeUnpin for SimulationRng
impl UnwindSafe for SimulationRng
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more