pub trait UniformSampler: Sized {
    type X;

    // Required methods
    fn new<B1, B2>(low: B1, high: B2) -> Result<Self, Error>
       where B1: SampleBorrow<Self::X>,
             B2: SampleBorrow<Self::X>;
    fn new_inclusive<B1, B2>(low: B1, high: B2) -> Result<Self, Error>
       where B1: SampleBorrow<Self::X>,
             B2: SampleBorrow<Self::X>;
    fn sample<R>(&self, rng: &mut R) -> Self::X
       where R: Rng + ?Sized;

    // Provided methods
    fn sample_single<R, B1, B2>(
        low: B1,
        high: B2,
        rng: &mut R
    ) -> Result<Self::X, Error>
       where R: Rng + ?Sized,
             B1: SampleBorrow<Self::X>,
             B2: SampleBorrow<Self::X> { ... }
    fn sample_single_inclusive<R, B1, B2>(
        low: B1,
        high: B2,
        rng: &mut R
    ) -> Result<Self::X, Error>
       where R: Rng + ?Sized,
             B1: SampleBorrow<Self::X>,
             B2: SampleBorrow<Self::X> { ... }
}
Expand description

Helper trait handling actual uniform sampling.

See the module documentation on how to implement Uniform range sampling for a custom type.

Implementation of sample_single is optional, and is only useful when the implementation can be faster than Self::new(low, high).sample(rng).

Required Associated Types§

source

type X

The type sampled by this implementation.

Required Methods§

source

fn new<B1, B2>(low: B1, high: B2) -> Result<Self, Error>
where B1: SampleBorrow<Self::X>, B2: SampleBorrow<Self::X>,

Construct self, with inclusive lower bound and exclusive upper bound [low, high).

Usually users should not call this directly but prefer to use Uniform::new.

source

fn new_inclusive<B1, B2>(low: B1, high: B2) -> Result<Self, Error>
where B1: SampleBorrow<Self::X>, B2: SampleBorrow<Self::X>,

Construct self, with inclusive bounds [low, high].

Usually users should not call this directly but prefer to use Uniform::new_inclusive.

source

fn sample<R>(&self, rng: &mut R) -> Self::X
where R: Rng + ?Sized,

Sample a value.

Provided Methods§

source

fn sample_single<R, B1, B2>( low: B1, high: B2, rng: &mut R ) -> Result<Self::X, Error>
where R: Rng + ?Sized, B1: SampleBorrow<Self::X>, B2: SampleBorrow<Self::X>,

Sample a single value uniformly from a range with inclusive lower bound and exclusive upper bound [low, high).

By default this is implemented using UniformSampler::new(low, high).sample(rng). However, for some types more optimal implementations for single usage may be provided via this method (which is the case for integers and floats). Results may not be identical.

Note that to use this method in a generic context, the type needs to be retrieved via SampleUniform::Sampler as follows:

use rand::{thread_rng, distributions::uniform::{SampleUniform, UniformSampler}};
fn sample_from_range<T: SampleUniform>(lb: T, ub: T) -> T {
    let mut rng = thread_rng();
    <T as SampleUniform>::Sampler::sample_single(lb, ub, &mut rng).unwrap()
}
source

fn sample_single_inclusive<R, B1, B2>( low: B1, high: B2, rng: &mut R ) -> Result<Self::X, Error>
where R: Rng + ?Sized, B1: SampleBorrow<Self::X>, B2: SampleBorrow<Self::X>,

Sample a single value uniformly from a range with inclusive lower bound and inclusive upper bound [low, high].

By default this is implemented using UniformSampler::new_inclusive(low, high).sample(rng). However, for some types more optimal implementations for single usage may be provided via this method. Results may not be identical.

Object Safety§

This trait is not object safe.

Implementors§