```
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§

## Required Methods§

Source#### fn new<B1, B2>(low: B1, high: B2) -> Result<Self, Error>

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

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

.

For discrete types (e.g. integers), samples will always be strictly less
than `high`

. For (approximations of) continuous types (e.g. `f32`

, `f64`

),
samples may equal `high`

due to loss of precision but may not be
greater than `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>

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

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

.

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

.

## Provided Methods§

Source#### fn sample_single<R, B1, B2>(
low: B1,
high: B2,
rng: &mut R,
) -> Result<Self::X, Error>

#### fn sample_single<R, B1, B2>( low: B1, high: B2, rng: &mut R, ) -> Result<Self::X, Error>

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

.

For discrete types (e.g. integers), samples will always be strictly less
than `high`

. For (approximations of) continuous types (e.g. `f32`

, `f64`

),
samples may equal `high`

due to loss of precision but may not be
greater than `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::distr::uniform::{SampleUniform, UniformSampler};
fn sample_from_range<T: SampleUniform>(lb: T, ub: T) -> T {
let mut rng = rand::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>

#### fn sample_single_inclusive<R, B1, B2>( low: B1, high: B2, rng: &mut R, ) -> Result<Self::X, Error>

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.

## Dyn Compatibility§

This trait is **not** dyn compatible.

*In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.*