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

Sample u32s uniformly from [0, bound).

Implementations§

source§

impl UniformIntegersUnderBound

source

pub fn new(bound: u32) -> Self

Create the distribution.

§Performance

This function performs very well as long as the bound, $b$ is such that $t(b)$ is small, where:

t(b) = (2^{32} - b + 1) \mod b

In particular, the probability that a random vector of $N$ elements is accepted is:

\left(1 - \frac{t(b)}{2^{32}}\right)^N

Thus, if $t(b)$ is quite small, then it is efficient for us to reject $N$ elements at a time, as opposed to rejecting individual elements.

§Repeated Invocations

It is inefficient to repeatedly call new with a fresh bound. However, an alternative algorithm has not yet been implemented.

§Timing Side-Channel

bound should be a public value, since it may be leaked in the timing of sampling.

§Panics

Panics if bound is 0.

source

pub fn bound(&self) -> u32

The exclusive bound of the integers produced by this generator.

source

pub fn sample(&self, rng: &mut AesRng) -> [U32x8; 4]

Produce Aes128EncryptOnly::BLOCK_COUNT_HINT * 4 uniformly distributed u32s (under the given bound).

source

pub fn sample_20(&self, rng: &mut AesRng) -> [U32x8; 3]

Produce 20 uniformly distributed u32s under the given bound.

Random numbers are returned in out[0][..], out[1][..], and the even-indexed entries of out[2] (i.e. out[2][0], out[2][2], out[2][4], out[2][6]).

§Alternatives

Consider using Self::sample instead. It may be faster on some platforms.

Trait Implementations§

source§

impl Clone for UniformIntegersUnderBound

source§

fn clone(&self) -> UniformIntegersUnderBound

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for UniformIntegersUnderBound

source§

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

Formats the value using the given formatter. Read more
source§

impl Copy for UniformIntegersUnderBound

Auto Trait Implementations§

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> Same for T

§

type Output = T

Should always be Self
source§

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

§

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

§

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

§

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V