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 VariableRangeGenerator::new; see its documentation for more.
Implementations§
Source§impl VariableRangeGenerator
impl VariableRangeGenerator
Sourcepub fn new(seed: Seed) -> Self
pub fn new(seed: Seed) -> Self
Generates unsigneds sampled from ranges. A single generator can sample from different ranges of different types.
If you only need to generate values from a single range, it is slightly more efficient to
use random_unsigned_bit_chunks, random_unsigneds_less_than,
random_unsigned_range, or random_unsigned_inclusive_range.
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::num::random::VariableRangeGenerator;
use malachite_base::random::EXAMPLE_SEED;
let mut generator = VariableRangeGenerator::new(EXAMPLE_SEED);
assert_eq!(generator.next_bit_chunk::<u16>(10), 881);
assert_eq!(generator.next_less_than::<u8>(100), 34);
assert_eq!(generator.next_in_range::<u32>(10, 20), 16);
assert_eq!(generator.next_in_inclusive_range::<u64>(10, 20), 14);Sourcepub fn next_bool(&mut self) -> bool
pub fn next_bool(&mut self) -> bool
Uniformly generates a bool.
$$ $P(\text{false}) = P(\text{true}) = \frac{1}{2}$. $$
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::num::random::VariableRangeGenerator;
use malachite_base::random::EXAMPLE_SEED;
let mut xs = Vec::with_capacity(10);
let mut generator = VariableRangeGenerator::new(EXAMPLE_SEED);
for _ in 0..10 {
xs.push(generator.next_bool());
}
assert_eq!(
xs,
&[true, false, true, false, true, true, true, true, true, false]
);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.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is chunk_size.
§Panics
Panics if chunk_size is zero or greater than the width of the type.
§Examples
use malachite_base::num::random::VariableRangeGenerator;
use malachite_base::random::EXAMPLE_SEED;
let mut xs = Vec::with_capacity(10);
let mut generator = VariableRangeGenerator::new(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.
§Expected complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is limit.significant_bits(). Each
rejection-sampling trial rejects with probability less than $1/2$, so the expected number of
trials is $O(1)$, but the worst case is unbounded.
§Panics
Panics if limit is 0.
§Examples
use malachite_base::num::random::VariableRangeGenerator;
use malachite_base::random::EXAMPLE_SEED;
let mut xs = Vec::with_capacity(10);
let mut generator = VariableRangeGenerator::new(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} $$
§Expected complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is (b - a).significant_bits(); the
worst case is unbounded, as with next_less_than.
§Panics
Panics if $a \geq b$.
§Examples
use malachite_base::num::random::VariableRangeGenerator;
use malachite_base::random::EXAMPLE_SEED;
let mut xs = Vec::with_capacity(10);
let mut generator = VariableRangeGenerator::new(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} $$
§Expected complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is the number of significant bits of
the range’s width; the worst case is unbounded, as with
next_less_than.
§Panics
Panics if $a > b$.
§Examples
use malachite_base::num::random::VariableRangeGenerator;
use malachite_base::random::EXAMPLE_SEED;
let mut xs = Vec::with_capacity(10);
let mut generator = VariableRangeGenerator::new(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§
Source§impl Clone for VariableRangeGenerator
impl Clone for VariableRangeGenerator
Source§fn clone(&self) -> VariableRangeGenerator
fn clone(&self) -> VariableRangeGenerator
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for VariableRangeGenerator
impl RefUnwindSafe for VariableRangeGenerator
impl Send for VariableRangeGenerator
impl Sync for VariableRangeGenerator
impl Unpin for VariableRangeGenerator
impl UnsafeUnpin for VariableRangeGenerator
impl UnwindSafe for VariableRangeGenerator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more