UniformIntegerSampler

Struct UniformIntegerSampler 

Source
pub struct UniformIntegerSampler { /* private fields */ }
Expand description

Enables uniformly random sampling a Z in [0, interval_size).

Attributes:

  • interval_size: defines the interval [0, interval_size), which we sample from
  • two_pow_32: is a helper to shift bits by 32-bits left by multiplication
  • nr_iterations: defines how many full samples of u32 are required
  • upper_modulo: is a power of two to remove superfluously sampled bits to increase the probability of accepting a sample to at least 1/2
  • rng: defines the ThreadRng that’s used to sample uniform u32 integers

§Examples

use qfall_math::{utils::sample::uniform::UniformIntegerSampler, integer::Z};
let interval_size = Z::from(20);

let mut uis = UniformIntegerSampler::init(&interval_size).unwrap();

let sample = uis.sample();

assert!(Z::ZERO <= sample);
assert!(sample < interval_size);

Implementations§

Source§

impl UniformIntegerSampler

Source

pub fn init(interval_size: &Z) -> Result<Self, MathError>

Initializes the UniformIntegerSampler with

  • interval_size as interval_size,
  • two_pow_32 as a u64 containing 2^32
  • nr_iterations as (interval_size - 1).bits() / 32 floored
  • upper_modulo as 2^{(interval_size - 1).bits() mod 32}
  • rng as a fresh ThreadRng

Parameters:

  • interval_size: specifies the interval [0, interval_size) from which the samples are drawn

Returns a UniformIntegerSampler or a MathError, if the interval size is chosen smaller than or equal to 1.

§Examples
use qfall_math::{utils::sample::uniform::UniformIntegerSampler, integer::Z};
let interval_size = Z::from(20);

let mut uis = UniformIntegerSampler::init(&interval_size).unwrap();
§Errors and Failures
Source

pub fn sample(&mut self) -> Z

Computes a uniformly chosen Z sample in [0, interval_size) using rejection sampling that accepts samples with probability at least 1/2.

§Examples
use qfall_math::{utils::sample::uniform::UniformIntegerSampler, integer::Z};
let interval_size = Z::from(20);

let mut uis = UniformIntegerSampler::init(&interval_size).unwrap();

let sample = uis.sample();

assert!(Z::ZERO <= sample);
assert!(sample < interval_size);
Source

pub fn sample_bits_uniform(&mut self) -> Z

Computes self.nr_iterations * 32 + upper_modulo many uniformly chosen bits.

Returns a Z containing self.nr_iterations * 32 + upper_modulo-many uniformly chosen bits.

§Examples
use qfall_math::{utils::sample::uniform::UniformIntegerSampler, integer::Z};
let interval = Z::from(u16::MAX) + 1;

let mut uis = UniformIntegerSampler::init(&interval).unwrap();

let sample = uis.sample_bits_uniform();

assert!(Z::ZERO <= sample);
assert!(sample < interval);

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

Source§

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

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

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

Source§

fn vzip(self) -> V