Function malachite_nz::integer::random::random_nonzero_integers

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

Generates random nonzero Integers whose absolute values have a specified mean bit length.

The actual signed bit length is chosen from a distribution that produces values whose mean absolute values are $m$, where $m$ is mean_bits_numerator / mean_bits_denominator (see geometric_random_nonzero_signeds); $m$ must be greater than 1. Then an Integer is chosen uniformly among all positive Integers with that bit length, and its sign is set to the sign of the signed bit length. The resulting distribution 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}{2m} \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_nonzero_integers;
use malachite_nz::integer::Integer;

assert_eq!(
    prefix_to_string(random_nonzero_integers(EXAMPLE_SEED, 32, 1), 10),
    "[6, 373973144, 46887963477285686350042496363292819122, -93254818, -126908, \
    -4471675267836600, 1860142159, -118004986915853475, -98, 346513, ...]"
)