Function spinoso_securerandom::random_number

source ·
pub fn random_number(max: Max) -> Result<Rand, DomainError>
Expand description

Generate a single random number, either a float or an integer.

In Ruby, the rand family of functions generate random numbers that are either floats or signed integers.

The random numbers returned by this function will never be negative and will always be finite.

In Ruby, the rand family of functions generate random numbers form within a range. This range is always anchored on the left by zero. See the Max enum documentation for how to bound the random numbers returned by this function.

§Examples

let rand = spinoso_securerandom::random_number(Max::None)?;
assert!(matches!(rand, Rand::Float(_)));

let rand = spinoso_securerandom::random_number(Max::Integer(57))?;
assert!(matches!(rand, Rand::Integer(_)));

let rand = spinoso_securerandom::random_number(Max::Integer(-20))?;
assert!(matches!(rand, Rand::Float(_)));

let rand = spinoso_securerandom::random_number(Max::Integer(0))?;
assert!(matches!(rand, Rand::Float(_)));

let rand = spinoso_securerandom::random_number(Max::Float(57.0))?;
assert!(matches!(rand, Rand::Float(_)));

let rand = spinoso_securerandom::random_number(Max::Float(-20.0))?;
assert!(matches!(rand, Rand::Float(_)));

let rand = spinoso_securerandom::random_number(Max::Float(0.0))?;
assert!(matches!(rand, Rand::Float(_)));

§Errors

If the float given in a Max::Float variant is NaN or infinite, a DomainError is returned.