pub struct Uniform<X>(_) 
where
    X: SampleUniform
;
Expand description

Sample values uniformly between two bounds.

Uniform::new and Uniform::new_inclusive construct a uniform distribution sampling from the given range; these functions may do extra work up front to make sampling of multiple values faster.

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 Standard distribution.

Steps are taken to avoid bias which might be present in naive implementations; for example rng.gen::<u8>() % 170 samples from the range [0, 169] but is twice as likely to select numbers less than 85 than other values. Further, the implementations here give more weight to the high-bits generated by the RNG than the low bits, since with some RNGs the low-bits are of lower quality than the high bits.

Implementations must sample in [low, high) range for Uniform::new(low, high), i.e., excluding high. In particular care must be taken to ensure that rounding never results values < low or >= high.

Example

use rand::distributions::{Distribution, Uniform};

fn main() {
    let between = Uniform::from(10..10000);
    let mut rng = rand::thread_rng();
    let mut sum = 0;
    for _ in 0..1000 {
        sum += between.sample(&mut rng);
    }
    println!("{}", sum);
}

Implementations

Create a new Uniform instance which samples uniformly from the half open range [low, high) (excluding high). Panics if low >= high.

Create a new Uniform instance which samples uniformly from the closed range [low, high] (inclusive). Panics if low > high.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Generate a random value of T, using rng as the source of randomness.

Create an iterator that generates random values of T, using rng as the source of randomness. Read more

Converts to this type from the input type.

Converts to this type from the input type.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Multiply and accumulate in this variable, formally *self += a * b.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more

Checks if self is actually part of its subset T (and can be converted to it).

Use with care! Same as self.to_subset but without any property checks. Always succeeds.

The inclusion map: converts self to the equivalent element of its superset.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.