Skip to main content

random_options

Function random_options 

Source
pub fn random_options<I: Iterator>(
    seed: Seed,
    none_p_numerator: u64,
    none_p_denominator: u64,
    xs_gen: &dyn Fn(Seed) -> I,
) -> RandomOptions<I> 
Expand description

Generates random Options with values from a given random iterator.

The probability of generating None is specified by $p$ = none_p_numerator / none_p_denominator. If a Some is generated, its values have the same distribution as the values generated by the given iterator.

If $Q(x)$ is the probability of $x$ being generated by xs, then

$P(\text{None}) = p$

$P(\operatorname{Some}(x)) = (1-p)Q(x)$

xs must be infinite.

The output length is infinite.

§Expected complexity per iteration

$T(i) = O(T^\prime(i))$

$M(i) = O(M^\prime(i))$

where $T$ is time, $M$ is additional memory, $i$ is the iteration number, and $T^\prime$ and $M^\prime$ are the time and memory functions of xs: each iteration adds only the weighted coin flip that decides between None and a wrapped value.

§Panics

Panics if none_p_denominator is 0 or none_p_numerator > none_p_denominator.

§Examples

use malachite_base::iterators::prefix_to_string;
use malachite_base::num::random::random_primitive_ints;
use malachite_base::options::random::random_options;
use malachite_base::random::EXAMPLE_SEED;
use malachite_base::strings::ToDebugString;

assert_eq!(
    prefix_to_string(
        random_options(EXAMPLE_SEED, 1, 2, &random_primitive_ints::<u8>)
            .map(|x| x.to_debug_string()),
        10
    ),
    "[Some(85), Some(11), Some(136), None, Some(200), None, Some(235), Some(134), Some(203), \
    None, ...]"
)