Skip to main content

EntropyPool

Struct EntropyPool 

Source
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>

Source

pub fn new() -> Self

Creates a new pool backed by rand::rng().

Source§

impl<R: Rng> EntropyPool<R>

Source

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.

Source

pub fn random_bytes_read(&self) -> u64

Returns the number of random bytes read from the backing RNG.

Source

pub fn retained_states(&self) -> u64

Returns the number of equiprobable states currently retained by the pool.

Source

pub fn retained_entropy_bits(&self) -> f64

Returns log2(retained_states()).

Source

pub fn rng_mut(&mut self) -> &mut R

Returns a mutable reference to the backing RNG.

Source

pub fn into_rng(self) -> R

Consumes the pool and returns the backing RNG.

Source

pub fn try_gen_range(&mut self, n: u32) -> Result<u32, EntropyPoolError>

Returns a uniform integer in 0..n.

Source

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.

Source

pub fn try_permutation( &mut self, m: u32, n: u32, ) -> Result<Vec<u32>, EntropyPoolError>

Returns m distinct values from 0..n in random order.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Default for EntropyPool<ThreadRng>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<R> Freeze for EntropyPool<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for EntropyPool<R>
where R: RefUnwindSafe,

§

impl<R> Send for EntropyPool<R>
where R: Send,

§

impl<R> Sync for EntropyPool<R>
where R: Sync,

§

impl<R> Unpin for EntropyPool<R>
where R: Unpin,

§

impl<R> UnsafeUnpin for EntropyPool<R>
where R: UnsafeUnpin,

§

impl<R> UnwindSafe for EntropyPool<R>
where R: UnwindSafe,

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.