Skip to main content

lib_jsl_random/distributions/
geometric.rs

1use crate::uniform_generator::UniformGenerator;
2
3/// The number of failures before the first success in independent trials of probability `p`
4/// (`P(k) = (1 − p)ᵏ p`), by inversion of the exponential: `⌊ln(1 − u) / ln(1 − p)⌋`.
5pub fn geometric_distribution<U>(uniform_generator: &mut U, p: f64) -> f64
6where
7    U: UniformGenerator,
8{
9    let uniform_sample: f64 = uniform_generator.next_f64();
10    ((1.0 - uniform_sample).ln() / (1.0 - p).ln()).floor()
11}