utilities/src/random.rs
2023-01-16 10:47:56 +01:00

43 lines
757 B
Rust

use rand::Rng;
pub struct Coin;
impl Coin {
pub fn flip(probability: f32) -> bool {
if probability >= 1.0 {
return true;
}
if probability <= 0.0 {
return false;
}
let random_number = Self::raw();
probability > random_number
}
pub fn raw() -> f32 {
rand::thread_rng().gen_range(0.0..1.0)
}
}
pub struct Random;
impl Random {
pub fn range(low: u32, high: u32) -> u32 {
if low == high {
return low;
}
rand::thread_rng().gen_range(low..high)
}
pub fn range_f32(low: f32, high: f32) -> f32 {
if low == high {
return low;
}
rand::thread_rng().gen_range(low..high)
}
}