Function malachite_nz::natural::random::random_positive_naturals

source ·
pub fn random_positive_naturals(
    seed: Seed,
    mean_bits_numerator: u64,
    mean_bits_denominator: u64
) -> RandomNaturals<GeometricRandomNaturalValues<u64>> 
Expand description

Generates random positive Naturals with a specified mean bit length.

The actual bit length is chosen from a geometric distribution with mean $m$, where $m$ is mean_bits_numerator / mean_bits_denominator; $m$ must be greater than 1. Then a Natural is chosen uniformly among all Naturals with that bit length. The resulting distribution resembles a Pareto distribution. It has no mean or higher-order statistics (unless $m < 2$, which is not typical).

$$ P(n) = \begin{cases} 0 & \text{if} \quad n = 0, \\ \frac{1}{m} \left ( \frac{m-1}{2m} \right ) ^ {\lfloor \log_2 n \rfloor} & \text{otherwise}. \end{cases} $$

The output length is infinite.

§Expected complexity per iteration

$T(n, m) = O(n + m)$

$M(n, m) = O(n / m)$

where $T$ is time, $M$ is additional memory, $n$ is mean_precision_numerator, and $m$ is mean_precision_denominator.

§Panics

Panics if mean_bits_numerator or mean_bits_denominator are zero or if mean_bits_numerator <= mean_bits_denominator.

§Examples

use malachite_base::iterators::prefix_to_string;
use malachite_base::random::EXAMPLE_SEED;
use malachite_nz::natural::random::random_positive_naturals;
use malachite_nz::natural::Natural;

assert_eq!(
    prefix_to_string(random_positive_naturals(EXAMPLE_SEED, 32, 1), 10),
    "[22, 4, 178, 55845661150, 93254818, 7577967529619388, 8, 11316951483471, 11, \
    1005760138411689342464923704482, ...]"
)