Trait UniformSampler
trait UniformSampler: Sized
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).
Associated Types
type XThe type sampled by this implementation.
Required Methods
fn new<B1, B2>(low: B1, high: B2) -> Result<Self, Error> where B1: SampleBorrow<<Self as >::X> + Sized, B2: SampleBorrow<<Self as >::X> + SizedConstruct 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 equalhighdue to loss of precision but may not be greater thanhigh.Usually users should not call this directly but prefer to use
Uniform::new.fn new_inclusive<B1, B2>(low: B1, high: B2) -> Result<Self, Error> where B1: SampleBorrow<<Self as >::X> + Sized, B2: SampleBorrow<<Self as >::X> + SizedConstruct self, with inclusive bounds
[low, high].Usually users should not call this directly but prefer to use
Uniform::new_inclusive.fn sample<R: Rng + ?Sized>(self: &Self, rng: &mut R) -> <Self as >::XSample a value.
Provided Methods
fn sample_single<R: Rng + ?Sized, B1, B2>(low: B1, high: B2, rng: &mut R) -> Result<<Self as >::X, Error> where B1: SampleBorrow<<Self as >::X> + Sized, B2: SampleBorrow<<Self as >::X> + SizedSample 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 equalhighdue to loss of precision but may not be greater thanhigh.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::Sampleras follows:use ; #fn sample_single_inclusive<R: Rng + ?Sized, B1, B2>(low: B1, high: B2, rng: &mut R) -> Result<<Self as >::X, Error> where B1: SampleBorrow<<Self as >::X> + Sized, B2: SampleBorrow<<Self as >::X> + SizedSample 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
impl UniformSampler for UniformUsizeimpl UniformSampler for UniformInt<u128>impl UniformSampler for UniformDurationimpl UniformSampler for UniformInt<u64>impl UniformSampler for UniformFloat<f64>impl UniformSampler for UniformInt<u32>impl UniformSampler for UniformFloat<f32>impl UniformSampler for UniformInt<u16>impl UniformSampler for UniformInt<u8>impl UniformSampler for UniformInt<i128>impl UniformSampler for UniformInt<i64>impl UniformSampler for UniformInt<i32>impl UniformSampler for UniformInt<i16>impl UniformSampler for UniformInt<i8>impl UniformSampler for UniformChar