Function spinoso_random::rand

source ·
pub fn rand(rng: &mut Random, max: Max) -> Result<Rand, ArgumentError>
Expand description

Generate random numbers bounded from below by 0 and above by the given max.

When max is an i64, rand returns a random integer greater than or equal to zero and less than max.

When max is an f64, rand returns a random floating point number between 0.0 and max, including 0.0 and excluding max.

§Implementation notes

This function does not yet support range constraints. When support is added, when max is a Range, rand will return a random number where range.member?(number) == true.

§Examples

Generate floats from (0.0..1.0):

use spinoso_random::{rand, ArgumentError, InitializeError, Max, Rand, Random};

let mut random = Random::new()?;
let max = Max::None;
let rand = rand(&mut random, max)?;
assert!(matches!(rand, Rand::Float(x) if x < 1.0));

Generate random integers:

use spinoso_random::{rand, ArgumentError, InitializeError, Max, Rand, Random};

let mut random = Random::new()?;
let max = Max::Integer(10);
let rand = rand(&mut random, max)?;
assert!(matches!(rand, Rand::Integer(x) if x < 10));

§Errors

When max is a negative integer or zero, rand returns an ArgumentError.

When max is a negative f64, rand returns an ArgumentError.

When max is a non-finite f64, rand returns a domain error ArgumentError.