common/
utils.rs

1use std::cell::Cell;
2
3/// Floating-point infinity.
4pub const INFINITY: f64 = f64::INFINITY;
5/// The mathematical constant π.
6pub const PI: f64 = std::f64::consts::PI;
7
8/// Converts degrees to radians.
9pub fn degrees_to_radians(degrees: f64) -> f64 {
10    degrees * PI / 180.0
11}
12
13// Xorshift64 PRNG (WASM-compatible)
14thread_local! {
15    static RNG_STATE: Cell<u64> = Cell::new(0x123456789abcdef0);
16}
17
18/// Returns a random f64 in the range `[0, 1)`.
19pub fn random_double() -> f64 {
20    RNG_STATE.with(|state| {
21        let mut s = state.get();
22        s ^= s << 13;
23        s ^= s >> 7;
24        s ^= s << 17;
25        state.set(s);
26        (s >> 11) as f64 * (1.0 / (1u64 << 53) as f64)
27    })
28}
29
30/// Returns a random f64 in the range `[min, max)`.
31pub fn random_double_range(min: f64, max: f64) -> f64 {
32    min + (max - min) * random_double()
33}