25 lines
496 B
Rust
25 lines
496 B
Rust
use rand::prelude::*;
|
|
use std::cell::RefCell;
|
|
|
|
thread_local! {
|
|
static RNG: RefCell<SmallRng> = RefCell::new(SmallRng::from_entropy())
|
|
}
|
|
|
|
/// generates random number 0<= x < 1
|
|
pub fn random_f64() -> f64 {
|
|
RNG.with(|rng| (*rng.borrow_mut()).gen())
|
|
}
|
|
|
|
pub fn random_rng(min: f64, max: f64) -> f64 {
|
|
min + (max - min) * random_f64()
|
|
}
|
|
|
|
pub fn clamp(x: f64, min: f64, max: f64) -> f64 {
|
|
if x < min {
|
|
return min;
|
|
}
|
|
if x > max {
|
|
return max;
|
|
}
|
|
return x;
|
|
}
|