Function malachite_nz::integer::random::random_natural_integers

source ·
pub fn random_natural_integers(
    seed: Seed,
    mean_bits_numerator: u64,
    mean_bits_denominator: u64
) -> RandomIntegers<GeometricRandomNaturalValues<i64>> 
Expand description

Generates random natural (non-negative) Integers 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 0. Then an Integer is chosen uniformly among all non-negative Integers with that bit length. The resulting distribution resembles a Pareto distribution. It has no mean or higher-order statistics (unless $m < 1$, which is not typical).

$$ P(n) = \begin{cases} 0 & \text{if} \quad n < 0, \\ \frac{1}{m + 1} & \text{if} \quad n = 0, \\ \frac{2}{m+1} \left ( \frac{m}{2(m+1)} \right ) ^ {\lfloor \log_2 n \rfloor + 1} & \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 after being reduced to lowest terms, their sum is greater than or equal to $2^{64}$.

§Examples

use malachite_base::iterators::prefix_to_string;
use malachite_base::random::EXAMPLE_SEED;
use malachite_nz::integer::random::random_natural_integers;
use malachite_nz::integer::Integer;

assert_eq!(
    prefix_to_string(random_natural_integers(EXAMPLE_SEED, 32, 1), 10),
    "[20431208470830262, 2777240, 114, 12184833305054, 1121025855008623490210, \
    13478874522577592, 115311695, 7, 18, 54522366353, ...]"
)