Skip to main content

Rng

Trait Rng 

Source
pub trait Rng: Send {
    // Required methods
    fn next_u64(&mut self) -> u64;
    fn fork(&mut self) -> Self
       where Self: Sized;

    // Provided methods
    fn next_u32(&mut self) -> u32 { ... }
    fn next_bool(&mut self) -> bool { ... }
    fn next_fixed(&mut self) -> FixedQ32 { ... }
    fn next_u64_bounded(&mut self, bound: u64) -> u64 { ... }
    fn choose<'a, T>(&mut self, slice: &'a [T]) -> Option<&'a T> { ... }
    fn duration_between(&mut self, min: Duration, max: Duration) -> Duration { ... }
}
Expand description

Trait for random number generation in protocol execution.

Implementations can provide deterministic seeded values for reproducible testing or host entropy in runtime contexts.

§Determinism

For simulation reproducibility, always use seeded implementations like SeededRng. The trait methods are designed to produce identical sequences given identical seeds.

Required Methods§

Source

fn next_u64(&mut self) -> u64

Generate a random u64.

Source

fn fork(&mut self) -> Self
where Self: Sized,

Fork into an independent RNG stream.

The forked RNG will produce a different sequence than the parent, enabling component isolation in simulation.

Provided Methods§

Source

fn next_u32(&mut self) -> u32

Generate a random u32.

Source

fn next_bool(&mut self) -> bool

Generate a random boolean.

Source

fn next_fixed(&mut self) -> FixedQ32

Generate a random FixedQ32 value in [0, 1).

Uses the upper 32 bits as the fractional part, giving uniform distribution over the representable range.

Source

fn next_u64_bounded(&mut self, bound: u64) -> u64

Generate a random u64 in the range [0, bound).

Uses rejection sampling for uniform distribution.

Source

fn choose<'a, T>(&mut self, slice: &'a [T]) -> Option<&'a T>

Choose a random element from a slice.

Source

fn duration_between(&mut self, min: Duration, max: Duration) -> Duration

Generate a random duration between min and max.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Rng for ChaCha20Rng

Source§

fn next_u64(&mut self) -> u64

Source§

fn fork(&mut self) -> ChaCha20Rng

Implementors§