Function malachite_base::num::random::geometric::geometric_random_positive_unsigneds
source · [−]pub fn geometric_random_positive_unsigneds<T: PrimitiveUnsigned>(
seed: Seed,
um_numerator: u64,
um_denominator: u64
) -> GeometricRandomNaturalValues<T>ⓘNotable traits for GeometricRandomNaturalValues<T>impl<T: PrimitiveInt> Iterator for GeometricRandomNaturalValues<T> type Item = T;
Expand description
Generates random positive unsigned integers from a truncated geometric distribution.
With this distribution, the probability of a value being generated decreases as the value
increases. The probabilities $P(1), P(2), P(3), \ldots$ decrease in a geometric sequence; that’s
where the “geometric” comes from. Unlike a true geometric distribution, this distribution is
truncated, meaning that values above T::MAX
are never generated.
The probabilities can drop more quickly or more slowly depending on a parameter $m_u$, called
the unadjusted mean. It is equal to um_numerator / um_denominator
. The unadjusted mean is
what the mean generated value would be if the distribution were not truncated. If $m_u$ is
significantly lower than T::MAX
, which is usually the case, then it is very close to the
actual mean. The higher $m_u$ is, the more gently the probabilities drop; the lower it is, the
more quickly they drop. $m_u$ must be greater than one. It may be arbitrarily high, but note
that the iteration time increases linearly with um_numerator + um_denominator
.
Here is a more precise characterization of this distribution. Let its support $S \subset \Z$ equal $[1, 2^W)$, where $W$ is the width of the type. Then we have $$ P(n) \neq 0 \leftrightarrow n \in S $$ and whenever $n, n + 1 \in S$, $$ \frac{P(n)}{P(n+1)} = \frac{m_u}{m_u - 1}. $$
The output length is infinite.
Expected complexity per iteration
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ = um_numerator + um_denominator
.
Panics
Panics if um_denominator
is zero or if um_numerator <= um_denominator
.
Examples
use malachite_base::iterators::prefix_to_string;
use malachite_base::num::random::geometric::geometric_random_positive_unsigneds;
use malachite_base::random::EXAMPLE_SEED;
assert_eq!(
prefix_to_string(geometric_random_positive_unsigneds::<u64>(EXAMPLE_SEED, 2, 1), 10),
"[2, 1, 1, 4, 5, 5, 2, 1, 1, 2, ...]"
)
Further details
Geometric distributions are more typically parametrized by a parameter $p$. The relationship between $p$ and $m_u$ is $m_u = \frac{1}{p}$, or $p = \frac{1}{m_u}$.
The probability mass function of this distribution is $$ P(n) = \begin{cases} \frac{(1-p)^{n-1}p}{1-(1-p)^{2^W-1}} & \text{if} \quad 0 < n < 2^W, \\ 0 & \text{otherwise}, \end{cases} $$ where $W$ is the width of the type.
It’s also useful to note that $$ \lim_{W \to \infty} P(n) = (1-p)^{n-1}p. $$