Function malachite_nz::natural::random::get_random_natural_with_bits
source · pub fn get_random_natural_with_bits(
xs: &mut RandomPrimitiveInts<u64>,
bits: u64
) -> Natural
Expand description
Generates a random Natural
with a given bit length.
The Natural
is 0 if $b$ is 0, or else chosen uniformly from $[2^{b-1}, 2^b)$.
$$ P(n) = \begin{cases} 1 & \text{if} \quad b = n = 0, \\ \frac{1}{2^{b-1}} & \text{if} \quad b \neq 0 \ \text{and} \ 2^{b-1} \leq n < 2^b, \\ 0 & \text{otherwise}. \end{cases} $$
§Expected complexity
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and n
is bits
.
§Examples
use malachite_base::num::random::random_primitive_ints;
use malachite_base::random::EXAMPLE_SEED;
use malachite_nz::natural::random::get_random_natural_with_bits;
assert_eq!(
get_random_natural_with_bits(&mut random_primitive_ints(EXAMPLE_SEED), 100).to_string(),
"976558340558744279591984426865"
);