pub struct Uniform<X>(/* private fields */)
where
    X: SampleUniform;Expand description
Sample values uniformly between two bounds.
§Construction
Uniform::new and Uniform::new_inclusive construct a uniform
distribution sampling from the given low and high limits. Uniform may
also be constructed via TryFrom as in Uniform::try_from(1..=6).unwrap().
Constructors may do extra work up front to allow faster sampling of multiple
values. Where only a single sample is required it is suggested to use
Rng::random_range or one of the sample_single methods instead.
When sampling from a constant range, many calculations can happen at
compile-time and all methods should be fast; for floating-point ranges and
the full range of integer types, this should have comparable performance to
the StandardUniform distribution.
§Provided implementations
- char(- UniformChar): samples a range over the implementation for- u32
- f32,- f64(- UniformFloat): samples approximately uniformly within a range; bias may be present in the least-significant bit of the significand and the limits of the input range may be sampled even when an open (exclusive) range is used
- Integer types (UniformInt) may show a small bias relative to the expected uniform distribution of output. In the worst case, bias affects 1 in2^nsamples where n is 56 (i8andu8), 48 (i16andu16), 96 (i32andu32), 64 (i64andu64), 128 (i128andu128). Theunbiasedfeature flag fixes this bias.
- usize(- UniformUsize) is handled specially, using the- u32implementation where possible to enable portable results across 32-bit and 64-bit CPU architectures.
- Duration(- UniformDuration): samples a range over the implementation for- u32or- u64
- SIMD types (requires simd_supportfeature) like x86’s__m128iandstd::simd’su32x4,f32x4andmask32x4types are effectively arrays of integer or floating-point types. Each lane is sampled independently from its own range, potentially with more efficient random-bit-usage than would be achieved with sequential sampling.
§Example
use rand::distr::{Distribution, Uniform};
let between = Uniform::try_from(10..10000).unwrap();
let mut rng = rand::rng();
let mut sum = 0;
for _ in 0..1000 {
    sum += between.sample(&mut rng);
}
println!("{}", sum);For a single sample, Rng::random_range may be preferred:
use rand::Rng;
let mut rng = rand::rng();
println!("{}", rng.random_range(0..10));Implementations§
Source§impl<X> Uniform<X>where
    X: SampleUniform,
 
impl<X> Uniform<X>where
    X: SampleUniform,
Sourcepub fn new<B1, B2>(low: B1, high: B2) -> Result<Uniform<X>, Error>where
    B1: SampleBorrow<X>,
    B2: SampleBorrow<X>,
 
pub fn new<B1, B2>(low: B1, high: B2) -> Result<Uniform<X>, Error>where
    B1: SampleBorrow<X>,
    B2: SampleBorrow<X>,
Create a new Uniform instance, which samples uniformly from the half
open range [low, high) (excluding 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.
Fails if low >= high, or if low, high or the range high - low is
non-finite. In release mode, only the range is checked.
Sourcepub fn new_inclusive<B1, B2>(low: B1, high: B2) -> Result<Uniform<X>, Error>where
    B1: SampleBorrow<X>,
    B2: SampleBorrow<X>,
 
pub fn new_inclusive<B1, B2>(low: B1, high: B2) -> Result<Uniform<X>, Error>where
    B1: SampleBorrow<X>,
    B2: SampleBorrow<X>,
Create a new Uniform instance, which samples uniformly from the closed
range [low, high] (inclusive).
Fails if low > high, or if low, high or the range high - low is
non-finite. In release mode, only the range is checked.
Trait Implementations§
Source§impl<'de, X> Deserialize<'de> for Uniform<X>
 
impl<'de, X> Deserialize<'de> for Uniform<X>
Source§fn deserialize<__D>(
    __deserializer: __D,
) -> Result<Uniform<X>, <__D as Deserializer<'de>>::Error>where
    __D: Deserializer<'de>,
 
fn deserialize<__D>(
    __deserializer: __D,
) -> Result<Uniform<X>, <__D as Deserializer<'de>>::Error>where
    __D: Deserializer<'de>,
Source§impl<X> Distribution<X> for Uniform<X>where
    X: SampleUniform,
 
impl<X> Distribution<X> for Uniform<X>where
    X: SampleUniform,
Source§impl SampleString for Uniform<char>
 
impl SampleString for Uniform<char>
Source§impl<X> Serialize for Uniform<X>
 
impl<X> Serialize for Uniform<X>
Source§fn serialize<__S>(
    &self,
    __serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
    __S: Serializer,
 
fn serialize<__S>(
    &self,
    __serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
    __S: Serializer,
Source§impl<X> TryFrom<RangeInclusive<X>> for Uniform<X>where
    X: SampleUniform,
 
impl<X> TryFrom<RangeInclusive<X>> for Uniform<X>where
    X: SampleUniform,
impl<X> Copy for Uniform<X>
impl<X> Eq for Uniform<X>
impl<X> StructuralPartialEq for Uniform<X>where
    X: SampleUniform,
Auto Trait Implementations§
impl<X> Freeze for Uniform<X>
impl<X> RefUnwindSafe for Uniform<X>
impl<X> Send for Uniform<X>
impl<X> Sync for Uniform<X>
impl<X> Unpin for Uniform<X>
impl<X> UnwindSafe for Uniform<X>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
 
impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
 
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CallInPlace<T> for Twhere
    T: CallInto<T>,
 
impl<T> CallInPlace<T> for Twhere
    T: CallInto<T>,
Source§fn call_inplace<F>(&mut self, f: F) -> <T as CallInto<T>>::Output
 
fn call_inplace<F>(&mut self, f: F) -> <T as CallInto<T>>::Output
call_on_mut method allows an object to be passed onto a function that takes a mutable reference
to the object. This is useful for cases where you want to perform an operation on
an object and mutate it in the process.Source§impl<T> CallInto<T> for T
 
impl<T> CallInto<T> for T
Source§impl<T> CallOn<T> for Twhere
    T: CallInto<T>,
 
impl<T> CallOn<T> for Twhere
    T: CallInto<T>,
Source§fn call_on<F>(&self, f: F) -> <T as CallInto<T>>::Output
 
fn call_on<F>(&self, f: F) -> <T as CallInto<T>>::Output
call_on method allows an object to be passed onto a function that takes a reference
to the object. This is useful for cases where you want to perform an operation on
an object without needing to extract it from a container or context.