Skip to main content

get_weighted_random_bool

Function get_weighted_random_bool 

Source
pub fn get_weighted_random_bool(
    range_generator: &mut VariableRangeGenerator,
    p_numerator: u64,
    p_denominator: u64,
) -> bool
Expand description

Generates a random bool with a particular probability of being true.

Let $n_p$ be p_numerator, $d_p$ be p_denominator, and let $p=n_p/d_p$. Then

$P(\text{true}) = p$,

$P(\text{false}) = 1-p$.

§Panics

Panics if p_denominator is 0 or p_numerator > p_denominator.

§Expected complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is p_denominator.significant_bits().

§Examples

use malachite_base::bools::random::get_weighted_random_bool;
use malachite_base::num::random::VariableRangeGenerator;
use malachite_base::random::EXAMPLE_SEED;

assert_eq!(
    get_weighted_random_bool(&mut VariableRangeGenerator::new(EXAMPLE_SEED), 1, 10),
    false
);