Function smartnoise_runtime::utilities::noise::sample_bit_prob[][src]

pub fn sample_bit_prob(prob: f64, enforce_constant_time: bool) -> Result<bool>

Sample a single bit with arbitrary probability of success

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

Arguments

Return

A bit that is 1 with probability “prob”

Examples

// returns a bit with Pr(bit = 1) = 0.7
use smartnoise_runtime::utilities::noise::sample_bit_prob;
let n = sample_bit_prob(0.7, false);
// fails because 1.3 not a valid probability
use smartnoise_runtime::utilities::noise::sample_bit_prob;
let n = sample_bit_prob(1.3, false);
// fails because -0.3 is not a valid probability
use smartnoise_runtime::utilities::noise::sample_bit_prob;
let n = sample_bit_prob(-0.3, false);