Skip to main content

lib_jsl_random/distributions/
bernoulli.rs

1use crate::uniform_generator::UniformGenerator;
2
3/// A Bernoulli sample: 1 with probability `p` (a uniform below `p`), 0 otherwise.
4pub fn bernoulli_distribution<U>(uniform_generator: &mut U, p: f64) -> f64
5where
6    U: UniformGenerator,
7{
8    let uniform_sample: f64 = uniform_generator.next_f64();
9    if uniform_sample < p { 1.0 } else { 0.0 }
10}