Struct malachite_base::num::random::VariableRangeGenerator
source · [−]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
sourceimpl VariableRangeGenerator
impl VariableRangeGenerator
sourcepub fn next_bit_chunk<T: PrimitiveUnsigned>(&mut self, chunk_size: u64) -> T
pub fn next_bit_chunk<T: PrimitiveUnsigned>(&mut self, chunk_size: u64) -> T
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]);
sourcepub fn next_less_than<T: PrimitiveUnsigned>(&mut self, limit: T) -> T
pub fn next_less_than<T: PrimitiveUnsigned>(&mut self, limit: T) -> T
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]);
sourcepub fn next_in_range<T: PrimitiveUnsigned>(&mut self, a: T, b: T) -> T
pub fn next_in_range<T: PrimitiveUnsigned>(&mut self, a: T, b: T) -> T
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]);
sourcepub fn next_in_inclusive_range<T: PrimitiveUnsigned>(&mut self, a: T, b: T) -> T
pub fn next_in_inclusive_range<T: PrimitiveUnsigned>(&mut self, a: T, b: T) -> T
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
sourceimpl Clone for VariableRangeGenerator
impl Clone for VariableRangeGenerator
sourcefn clone(&self) -> VariableRangeGenerator
fn clone(&self) -> VariableRangeGenerator
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
Auto Trait Implementations
impl RefUnwindSafe for VariableRangeGenerator
impl Send for VariableRangeGenerator
impl Sync for VariableRangeGenerator
impl Unpin for VariableRangeGenerator
impl UnwindSafe for VariableRangeGenerator
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more