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§
sourcefn sample_bernoulli(prob: T, constant_time: bool) -> Fallible<Self>
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.