Struct scuttlebutt::UniformIntegersUnderBound
source · pub struct UniformIntegersUnderBound { /* private fields */ }
Expand description
Sample u32
s uniformly from [0, bound)
.
Implementations§
source§impl UniformIntegersUnderBound
impl UniformIntegersUnderBound
sourcepub fn new(bound: u32) -> Self
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
.
sourcepub fn sample(&self, rng: &mut AesRng) -> [U32x8; 4]
pub fn sample(&self, rng: &mut AesRng) -> [U32x8; 4]
Produce Aes128EncryptOnly::BLOCK_COUNT_HINT * 4
uniformly distributed u32
s (under the
given bound).
sourcepub fn sample_20(&self, rng: &mut AesRng) -> [U32x8; 3]
pub fn sample_20(&self, rng: &mut AesRng) -> [U32x8; 3]
Produce 20 uniformly distributed u32
s 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
impl Clone for UniformIntegersUnderBound
source§fn clone(&self) -> UniformIntegersUnderBound
fn clone(&self) -> UniformIntegersUnderBound
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more