pub trait RandomNumberGenerator {
    // Required methods
    fn next_u64(&mut self) -> u64;
    fn random_data(&mut self, size: usize) -> Vec<u8>;
    fn fill_random_data(&mut self, data: &mut [u8]);

    // Provided methods
    fn next_with_upper_bound<T>(&mut self, upper_bound: T) -> T
       where T: PrimInt + Unsigned + NumCast + FromPrimitive + AsPrimitive<u128> + OverflowingMul + Shl<usize, Output = T> + Shr<usize, Output = T> + WrappingSub + OverflowingAdd + Widening { ... }
    fn next_in_range<T>(&mut self, range: &Range<T>) -> T
       where T: PrimInt + FromPrimitive + AsPrimitive<u128> + OverflowingMul + Shl<usize, Output = T> + Shr<usize, Output = T> + HasMagnitude + OverflowingAdd { ... }
    fn next_in_closed_range<T>(&mut self, range: &RangeInclusive<T>) -> T
       where T: PrimInt + FromPrimitive + AsPrimitive<u128> + OverflowingMul + Shl<usize, Output = T> + Shr<usize, Output = T> + HasMagnitude { ... }
}
Expand description

A type that can generate random numbers.

Required Methods§

source

fn next_u64(&mut self) -> u64

Returns the next random u64.

source

fn random_data(&mut self, size: usize) -> Vec<u8>

Returns a vector of random bytes of the given size.

source

fn fill_random_data(&mut self, data: &mut [u8])

Fills the given buffer with random bytes.

Provided Methods§

source

fn next_with_upper_bound<T>(&mut self, upper_bound: T) -> Twhere T: PrimInt + Unsigned + NumCast + FromPrimitive + AsPrimitive<u128> + OverflowingMul + Shl<usize, Output = T> + Shr<usize, Output = T> + WrappingSub + OverflowingAdd + Widening,

Returns a random value that is less than the given upper bound.

Use this method when you need random binary data to generate another value. If you need an integer value within a specific range, use the static random(in:using:) method on that integer type instead of this method.

  • Parameter upperBound: The upper bound for the randomly generated value. Must be non-zero.
  • Returns: A random value of T in the range 0..<upperBound. Every value in the range 0..<upperBound is equally likely to be returned.
source

fn next_in_range<T>(&mut self, range: &Range<T>) -> Twhere T: PrimInt + FromPrimitive + AsPrimitive<u128> + OverflowingMul + Shl<usize, Output = T> + Shr<usize, Output = T> + HasMagnitude + OverflowingAdd,

Returns a random value within the specified range, using the given generator as a source for randomness.

Use this method to generate an integer within a specific range when you are using a custom random number generator. This example creates three new values in the range 1...100.

  • Parameters:
    • range: The range in which to create a random value.
    • generator: The random number generator to use when creating the new random value.
  • Returns: A random value within the bounds of range.
source

fn next_in_closed_range<T>(&mut self, range: &RangeInclusive<T>) -> Twhere T: PrimInt + FromPrimitive + AsPrimitive<u128> + OverflowingMul + Shl<usize, Output = T> + Shr<usize, Output = T> + HasMagnitude,

Implementors§