Function malachite_nz::integer::random::random_positive_integers
source · pub fn random_positive_integers(
seed: Seed,
mean_bits_numerator: u64,
mean_bits_denominator: u64
) -> RandomIntegers<GeometricRandomNaturalValues<i64>> ⓘ
Expand description
Generates random positive Integer
s 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 an Integer
is chosen uniformly among all positive Integer
s 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 \leq 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::integer::random::random_positive_integers;
assert_eq!(
prefix_to_string(random_positive_integers(EXAMPLE_SEED, 32, 1), 10),
"[22, 4, 178, 55845661150, 93254818, 7577967529619388, 8, 11316951483471, 11, \
1005760138411689342464923704482, ...]"
)