Skip to main content

VariableRangeGenerator

Struct 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 VariableRangeGenerator::new; see its documentation for more.

Implementations§

Source§

impl VariableRangeGenerator

Source

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);
Source

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]
);
Source

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]);
Source

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]);
Source

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]);
Source

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

Source§

fn clone(&self) -> VariableRangeGenerator

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for VariableRangeGenerator

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T, U> ExactFrom<T> for U
where U: TryFrom<T>,

Source§

fn exact_from(value: T) -> U

Source§

impl<T, U> ExactInto<U> for T
where U: ExactFrom<T>,

Source§

fn exact_into(self) -> U

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, U> OverflowingInto<U> for T
where U: OverflowingFrom<T>,

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> RoundingInto<U> for T
where U: RoundingFrom<T>,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> SaturatingInto<U> for T
where U: SaturatingFrom<T>,

Source§

impl<T> ToDebugString for T
where T: Debug,

Source§

fn to_debug_string(&self) -> String

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)");
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T, U> WrappingInto<U> for T
where U: WrappingFrom<T>,

Source§

fn wrapping_into(self) -> U