pub struct EntropyPool<R = ThreadRng> { /* private fields */ }Expand description
A bounded sampler that reuses residual entropy between draws.
The default type parameter uses rand::rngs::ThreadRng. Use
EntropyPool::with_rng to provide a deterministic or custom RNG.
Implementations§
Source§impl EntropyPool<ThreadRng>
impl EntropyPool<ThreadRng>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new pool backed by rand::rng().
Source§impl<R: Rng> EntropyPool<R>
impl<R: Rng> EntropyPool<R>
Sourcepub fn with_rng(rng: R) -> Self
pub fn with_rng(rng: R) -> Self
Creates a new pool backed by rng.
The pool reads one random byte immediately so that its initial state is
uniform on 0..256.
Sourcepub fn random_bytes_read(&self) -> u64
pub fn random_bytes_read(&self) -> u64
Returns the number of random bytes read from the backing RNG.
Sourcepub fn retained_states(&self) -> u64
pub fn retained_states(&self) -> u64
Returns the number of equiprobable states currently retained by the pool.
Sourcepub fn retained_entropy_bits(&self) -> f64
pub fn retained_entropy_bits(&self) -> f64
Returns log2(retained_states()).
Sourcepub fn try_gen_range(&mut self, n: u32) -> Result<u32, EntropyPoolError>
pub fn try_gen_range(&mut self, n: u32) -> Result<u32, EntropyPoolError>
Returns a uniform integer in 0..n.
Sourcepub fn gen_range(&mut self, n: u32) -> u32
pub fn gen_range(&mut self, n: u32) -> u32
Returns a uniform integer in 0..n.
Panics if n == 0. Use Self::try_gen_range to handle invalid input
without panicking.
Sourcepub fn try_permutation(
&mut self,
m: u32,
n: u32,
) -> Result<Vec<u32>, EntropyPoolError>
pub fn try_permutation( &mut self, m: u32, n: u32, ) -> Result<Vec<u32>, EntropyPoolError>
Returns m distinct values from 0..n in random order.
Sourcepub fn permutation(&mut self, m: u32, n: u32) -> Vec<u32>
pub fn permutation(&mut self, m: u32, n: u32) -> Vec<u32>
Returns m distinct values from 0..n in random order.
Panics if m > n. Use Self::try_permutation to handle invalid input
without panicking.
Sourcepub fn try_combination(
&mut self,
m: u32,
n: u32,
) -> Result<BTreeSet<u32>, EntropyPoolError>
pub fn try_combination( &mut self, m: u32, n: u32, ) -> Result<BTreeSet<u32>, EntropyPoolError>
Returns m distinct values from 0..n as a sorted set.
Sourcepub fn combination(&mut self, m: u32, n: u32) -> BTreeSet<u32>
pub fn combination(&mut self, m: u32, n: u32) -> BTreeSet<u32>
Returns m distinct values from 0..n as a sorted set.
Panics if m > n. Use Self::try_combination to handle invalid input
without panicking.