[][src]Function turtle::rand::random_range

pub fn random_range<T: RandomRange<B>, B>(low: B, high: B) -> T

Generates a random value in the given range.

The value x that is returned will be such that low ≤ x ≤ high.

Most types that can be used with random() can also be used with this function. For a list of types that can be passed into this function, see the documentation for the RandomRange trait.

Panics

Panics if low > high

Example:

use turtle::rand::random_range;

// Generates an f64 value between 100 and 199.99999...
let value: f64 = random_range(100.0, 200.0);
assert!(value >= 100.0 && value <= 200.0);

// Generates a u64 value between 1000 and 3000000
let value = random_range::<u64, _>(1000, 3000001);
assert!(value >= 1000 && value <= 3000001);

// You do not need to specify the type if the compiler has enough information:
fn foo(a: u64) {}
foo(random_range(432, 1938));

// This will always return the same value because `low == `high`
assert_eq!(random_range::<i32, _>(10i32, 10), 10);