pub trait SampleBernoulli<T>: Sized {
    // Required method
    fn sample_bernoulli(prob: T, constant_time: bool) -> Fallible<Self>;
}
Expand description

Sample a single bit with arbitrary probability of success.

Arguments

  • prob- The desired probability of success (bit = 1).
  • constant_time - Whether or not to enforce the algorithm to run in constant time

Return

A true boolean with probability “prob”.

Examples

// returns a true with Pr(bit = 1) = 0.7
use opendp::traits::samplers::SampleBernoulli;
let n = bool::sample_bernoulli(0.7, false);
// fails because 1.3 not a valid probability
use opendp::traits::samplers::SampleBernoulli;
let n = bool::sample_bernoulli(1.3, false);
// fails because -0.3 is not a valid probability
use opendp::traits::samplers::SampleBernoulli;
let n = bool::sample_bernoulli(-0.3, false);

Required Methods§

source

fn sample_bernoulli(prob: T, constant_time: bool) -> Fallible<Self>

Proof Definition

For any setting of prob, returns Ok(out), where out is a sample from the Bernoulli(prob) distribution, or Err(e) if there is not enough system entropy.

If constant_time is set, the algorithm should also run in constant time.

Implementations on Foreign Types§

source§

impl<T> SampleBernoulli<T> for boolwhere T: One + Zero + PartialOrd + FloatBits, T::Bits: PartialOrd + ExactIntCast<usize>, usize: ExactIntCast<T::Bits>,

source§

fn sample_bernoulli(prob: T, constant_time: bool) -> Fallible<Self>

Uses only an unbiased source of coin flips. The strategy for doing this with 2 flips in expectation is described here.

source§

impl SampleBernoulli<Rational> for bool

source§

fn sample_bernoulli(prob: Rational, constant_time: bool) -> Fallible<bool>

Implementors§