pub trait UniformSampler: Sized {
type X;
// Required methods
fn new<B1, B2>(low: B1, high: B2) -> Result<Self, Error>
where B1: SampleBorrow<Self::X> + Sized,
B2: SampleBorrow<Self::X> + Sized;
fn new_inclusive<B1, B2>(low: B1, high: B2) -> Result<Self, Error>
where B1: SampleBorrow<Self::X> + Sized,
B2: SampleBorrow<Self::X> + Sized;
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::X;
// Provided methods
fn sample_single<R: Rng + ?Sized, B1, B2>(
low: B1,
high: B2,
rng: &mut R,
) -> Result<Self::X, Error>
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,
) -> Result<Self::X, Error>
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§
Required Methods§
Sourcefn 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
.
Sourcefn 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§
Sourcefn sample_single<R: Rng + ?Sized, B1, B2>(
low: B1,
high: B2,
rng: &mut R,
) -> Result<Self::X, Error>
fn sample_single<R: Rng + ?Sized, 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()
}
Sourcefn sample_single_inclusive<R: Rng + ?Sized, B1, B2>(
low: B1,
high: B2,
rng: &mut R,
) -> Result<Self::X, Error>
fn sample_single_inclusive<R: Rng + ?Sized, 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.
Implementors§
Source§impl UniformSampler for UniformChar
impl UniformSampler for UniformChar
Source§impl UniformSampler for UniformDuration
impl UniformSampler for UniformDuration
Source§impl UniformSampler for UniformFloat<f32>
impl UniformSampler for UniformFloat<f32>
Source§impl UniformSampler for UniformFloat<f64>
impl UniformSampler for UniformFloat<f64>
Source§impl UniformSampler for UniformFloat<f32x2>
Available on crate feature simd_support
only.
impl UniformSampler for UniformFloat<f32x2>
simd_support
only.Source§impl UniformSampler for UniformFloat<f32x4>
Available on crate feature simd_support
only.
impl UniformSampler for UniformFloat<f32x4>
simd_support
only.Source§impl UniformSampler for UniformFloat<f32x8>
Available on crate feature simd_support
only.
impl UniformSampler for UniformFloat<f32x8>
simd_support
only.Source§impl UniformSampler for UniformFloat<f32x16>
Available on crate feature simd_support
only.
impl UniformSampler for UniformFloat<f32x16>
simd_support
only.Source§impl UniformSampler for UniformFloat<f64x2>
Available on crate feature simd_support
only.
impl UniformSampler for UniformFloat<f64x2>
simd_support
only.Source§impl UniformSampler for UniformFloat<f64x4>
Available on crate feature simd_support
only.
impl UniformSampler for UniformFloat<f64x4>
simd_support
only.Source§impl UniformSampler for UniformFloat<f64x8>
Available on crate feature simd_support
only.
impl UniformSampler for UniformFloat<f64x8>
simd_support
only.Source§impl UniformSampler for UniformInt<i8>
impl UniformSampler for UniformInt<i8>
Source§impl UniformSampler for UniformInt<i16>
impl UniformSampler for UniformInt<i16>
Source§impl UniformSampler for UniformInt<i32>
impl UniformSampler for UniformInt<i32>
Source§impl UniformSampler for UniformInt<i64>
impl UniformSampler for UniformInt<i64>
Source§impl UniformSampler for UniformInt<i128>
impl UniformSampler for UniformInt<i128>
Source§impl UniformSampler for UniformInt<u8>
impl UniformSampler for UniformInt<u8>
Source§impl UniformSampler for UniformInt<u16>
impl UniformSampler for UniformInt<u16>
Source§impl UniformSampler for UniformInt<u32>
impl UniformSampler for UniformInt<u32>
Source§impl UniformSampler for UniformInt<u64>
impl UniformSampler for UniformInt<u64>
Source§impl UniformSampler for UniformInt<u128>
impl UniformSampler for UniformInt<u128>
Source§impl UniformSampler for UniformUsize
Available on 32-bit or 64-bit only.
impl UniformSampler for UniformUsize
Source§impl<const LANES: usize> UniformSampler for UniformInt<Simd<i8, LANES>>where
LaneCount<LANES>: SupportedLaneCount,
Simd<u8, LANES>: WideningMultiply<Output = (Simd<u8, LANES>, Simd<u8, LANES>)>,
StandardUniform: Distribution<Simd<u8, LANES>>,
Available on crate feature simd_support
only.
impl<const LANES: usize> UniformSampler for UniformInt<Simd<i8, LANES>>where
LaneCount<LANES>: SupportedLaneCount,
Simd<u8, LANES>: WideningMultiply<Output = (Simd<u8, LANES>, Simd<u8, LANES>)>,
StandardUniform: Distribution<Simd<u8, LANES>>,
simd_support
only.Source§impl<const LANES: usize> UniformSampler for UniformInt<Simd<i16, LANES>>where
LaneCount<LANES>: SupportedLaneCount,
Simd<u16, LANES>: WideningMultiply<Output = (Simd<u16, LANES>, Simd<u16, LANES>)>,
StandardUniform: Distribution<Simd<u16, LANES>>,
Available on crate feature simd_support
only.
impl<const LANES: usize> UniformSampler for UniformInt<Simd<i16, LANES>>where
LaneCount<LANES>: SupportedLaneCount,
Simd<u16, LANES>: WideningMultiply<Output = (Simd<u16, LANES>, Simd<u16, LANES>)>,
StandardUniform: Distribution<Simd<u16, LANES>>,
simd_support
only.Source§impl<const LANES: usize> UniformSampler for UniformInt<Simd<i32, LANES>>where
LaneCount<LANES>: SupportedLaneCount,
Simd<u32, LANES>: WideningMultiply<Output = (Simd<u32, LANES>, Simd<u32, LANES>)>,
StandardUniform: Distribution<Simd<u32, LANES>>,
Available on crate feature simd_support
only.
impl<const LANES: usize> UniformSampler for UniformInt<Simd<i32, LANES>>where
LaneCount<LANES>: SupportedLaneCount,
Simd<u32, LANES>: WideningMultiply<Output = (Simd<u32, LANES>, Simd<u32, LANES>)>,
StandardUniform: Distribution<Simd<u32, LANES>>,
simd_support
only.Source§impl<const LANES: usize> UniformSampler for UniformInt<Simd<i64, LANES>>where
LaneCount<LANES>: SupportedLaneCount,
Simd<u64, LANES>: WideningMultiply<Output = (Simd<u64, LANES>, Simd<u64, LANES>)>,
StandardUniform: Distribution<Simd<u64, LANES>>,
Available on crate feature simd_support
only.
impl<const LANES: usize> UniformSampler for UniformInt<Simd<i64, LANES>>where
LaneCount<LANES>: SupportedLaneCount,
Simd<u64, LANES>: WideningMultiply<Output = (Simd<u64, LANES>, Simd<u64, LANES>)>,
StandardUniform: Distribution<Simd<u64, LANES>>,
simd_support
only.Source§impl<const LANES: usize> UniformSampler for UniformInt<Simd<u8, LANES>>where
LaneCount<LANES>: SupportedLaneCount,
Simd<u8, LANES>: WideningMultiply<Output = (Simd<u8, LANES>, Simd<u8, LANES>)>,
StandardUniform: Distribution<Simd<u8, LANES>>,
Available on crate feature simd_support
only.
impl<const LANES: usize> UniformSampler for UniformInt<Simd<u8, LANES>>where
LaneCount<LANES>: SupportedLaneCount,
Simd<u8, LANES>: WideningMultiply<Output = (Simd<u8, LANES>, Simd<u8, LANES>)>,
StandardUniform: Distribution<Simd<u8, LANES>>,
simd_support
only.Source§impl<const LANES: usize> UniformSampler for UniformInt<Simd<u16, LANES>>where
LaneCount<LANES>: SupportedLaneCount,
Simd<u16, LANES>: WideningMultiply<Output = (Simd<u16, LANES>, Simd<u16, LANES>)>,
StandardUniform: Distribution<Simd<u16, LANES>>,
Available on crate feature simd_support
only.
impl<const LANES: usize> UniformSampler for UniformInt<Simd<u16, LANES>>where
LaneCount<LANES>: SupportedLaneCount,
Simd<u16, LANES>: WideningMultiply<Output = (Simd<u16, LANES>, Simd<u16, LANES>)>,
StandardUniform: Distribution<Simd<u16, LANES>>,
simd_support
only.Source§impl<const LANES: usize> UniformSampler for UniformInt<Simd<u32, LANES>>where
LaneCount<LANES>: SupportedLaneCount,
Simd<u32, LANES>: WideningMultiply<Output = (Simd<u32, LANES>, Simd<u32, LANES>)>,
StandardUniform: Distribution<Simd<u32, LANES>>,
Available on crate feature simd_support
only.
impl<const LANES: usize> UniformSampler for UniformInt<Simd<u32, LANES>>where
LaneCount<LANES>: SupportedLaneCount,
Simd<u32, LANES>: WideningMultiply<Output = (Simd<u32, LANES>, Simd<u32, LANES>)>,
StandardUniform: Distribution<Simd<u32, LANES>>,
simd_support
only.Source§impl<const LANES: usize> UniformSampler for UniformInt<Simd<u64, LANES>>where
LaneCount<LANES>: SupportedLaneCount,
Simd<u64, LANES>: WideningMultiply<Output = (Simd<u64, LANES>, Simd<u64, LANES>)>,
StandardUniform: Distribution<Simd<u64, LANES>>,
Available on crate feature simd_support
only.
impl<const LANES: usize> UniformSampler for UniformInt<Simd<u64, LANES>>where
LaneCount<LANES>: SupportedLaneCount,
Simd<u64, LANES>: WideningMultiply<Output = (Simd<u64, LANES>, Simd<u64, LANES>)>,
StandardUniform: Distribution<Simd<u64, LANES>>,
simd_support
only.