# UniformSampler in rand::distributions::uniform - Rust
Helper trait handling actual uniform sampling.
Keywords: rust, rustlang, rust-lang, UniformSampler

# Trait rand::distributions::uniform::UniformSampler

source · [−]

    pub trait UniformSampler: Sized {
        type X;

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

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

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) -> Self where
B1: SampleBorrow<Self::X> + Sized,
B2: SampleBorrow<Self::X> + Sized,

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

Usually users should not call this directly but instead use `Uniform::new`, which asserts that `low < high` before calling this.

source

#### fn new_inclusive<B1, B2>(low: B1, high: B2) -> Self where
B1: SampleBorrow<Self::X> + Sized,
B2: SampleBorrow<Self::X> + Sized,

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

Usually users should not call this directly but instead use `Uniform::new_inclusive`, which asserts that `low <= high` before calling this.

source

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

Sample a value.

## Provided Methods

source

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

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)
    }

source

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

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.

## Implementors

source

### impl UniformSampler for UniformChar

#### type X = char

source

### impl UniformSampler for UniformDuration

#### type X = Duration

source

### impl UniformSampler for UniformFloat<f32>

#### type X = f32

source

### impl UniformSampler for UniformFloat<f64>

#### type X = f64

source

### impl UniformSampler for UniformInt<i8>

#### type X = i8

source

### impl UniformSampler for UniformInt<i16>

#### type X = i16

source

### impl UniformSampler for UniformInt<i32>

#### type X = i32

source

### impl UniformSampler for UniformInt<i64>

#### type X = i64

source

### impl UniformSampler for UniformInt<i128>

#### type X = i128

source

### impl UniformSampler for UniformInt<isize>

#### type X = isize

source

### impl UniformSampler for UniformInt<u8>

#### type X = u8

source

### impl UniformSampler for UniformInt<u16>

#### type X = u16

source

### impl UniformSampler for UniformInt<u32>

#### type X = u32

source

### impl UniformSampler for UniformInt<u64>

#### type X = u64

source

### impl UniformSampler for UniformInt<u128>

#### type X = u128

source

### impl UniformSampler for UniformInt<usize>

#### type X = usize