pub struct VariableRangeGenerator { /* private fields */ }
Expand description

Generates unsigneds sampled from ranges. A single generator can sample from different ranges of different types.

This struct is created by variable_range_generator; see its documentation for more.

Implementations

Uniformly generates an unsigned integer with up to some number of bits.

$$ P(x) = \begin{cases} 2^{-c} & \text{if} \quad 0 \leq x < 2^c, \\ 0 & \text{if} \quad \text{otherwise,} \end{cases} $$ where $c$ is chunk_size.

The output length is infinite.

Worst-case complexity

Constant time and additional memory.

Panics

Panics if chunk_size is zero or greater than the width of the type.

Examples
use malachite_base::num::random::variable_range_generator;
use malachite_base::random::EXAMPLE_SEED;

let mut xs = Vec::with_capacity(10);
let mut generator = variable_range_generator(EXAMPLE_SEED);
for _ in 0..10 {
    xs.push(generator.next_bit_chunk::<u8>(3));
}
assert_eq!(xs, &[1, 6, 5, 7, 6, 3, 1, 2, 4, 5]);

Uniformly generates a random unsigned integer less than a positive limit.

$$ P(x) = \begin{cases} \frac{1}{\ell} & \text{if} \quad x < \ell \\ 0 & \text{otherwise} \end{cases} $$ where $\ell$ is limit.

Worst-case complexity

Constant time and additional memory.

Panics

Panics if limit is 0.

Examples
use malachite_base::num::random::variable_range_generator;
use malachite_base::random::EXAMPLE_SEED;

let mut xs = Vec::with_capacity(10);
let mut generator = variable_range_generator(EXAMPLE_SEED);
for _ in 0..10 {
    xs.push(generator.next_less_than(10u8));
}
assert_eq!(xs, &[1, 7, 5, 4, 6, 4, 2, 8, 1, 7]);

Uniformly generates a random unsigned integer in the half-open interval $[a, b)$.

$a$ must be less than $b$. This function cannot create a range that includes T::MAX; for that, use next_in_inclusive_range.

$$ P(x) = \begin{cases} \frac{1}{b-a} & \text{if} \quad a \leq x < b, \\ 0 & \text{otherwise.} \end{cases} $$

Worst-case complexity

Constant time and additional memory.

Panics

Panics if $a \geq b$.

Examples
use malachite_base::num::random::variable_range_generator;
use malachite_base::random::EXAMPLE_SEED;

let mut xs = Vec::with_capacity(10);
let mut generator = variable_range_generator(EXAMPLE_SEED);
for _ in 0..10 {
    xs.push(generator.next_in_range(10u8, 20));
}
assert_eq!(xs, &[11, 17, 15, 14, 16, 14, 12, 18, 11, 17]);

Uniformly generates a random unsigned integer in the closed interval $[a, b]$.

$a$ must be less than or equal to $b$.

$$ P(x) = \begin{cases} \frac{1}{b-a+1} & \text{if} \quad a \leq x \leq b, \\ 0 & \text{otherwise.} \end{cases} $$

Worst-case complexity

Constant time and additional memory.

Panics

Panics if $a > b$.

Examples
use malachite_base::num::random::variable_range_generator;
use malachite_base::random::EXAMPLE_SEED;

let mut xs = Vec::with_capacity(10);
let mut generator = variable_range_generator(EXAMPLE_SEED);
for _ in 0..10 {
    xs.push(generator.next_in_inclusive_range(10u8, 19));
}
assert_eq!(xs, &[11, 17, 15, 14, 16, 14, 12, 18, 11, 17]);

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

Returns the String produced by Ts Debug implementation.

Examples
use malachite_base::strings::ToDebugString;

assert_eq!([1, 2, 3].to_debug_string(), "[1, 2, 3]");
assert_eq!(
    [vec![2, 3], vec![], vec![4]].to_debug_string(),
    "[[2, 3], [], [4]]"
);
assert_eq!(Some(5).to_debug_string(), "Some(5)");

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.