radiant_utils/misc/
arng.rs1use std::sync::atomic::{AtomicUsize, Ordering};
2
3pub struct ARng (AtomicUsize);
5
6impl ARng {
7
8 pub fn new(seed: usize) -> ARng {
10 ARng(AtomicUsize::new(seed))
11 }
12
13 pub fn get(self: &Self) -> f64 {
15 let pos = self.0.fetch_add(1, Ordering::SeqCst);
16 let large = (pos as f64).sin() * 100000000.0;
17 large - large.floor()
18 }
19
20 pub fn range(self: &Self, min: f64, max: f64) -> f64 {
22 let pos = self.0.fetch_add(1, Ordering::SeqCst);
23 let large = (pos as f64).sin() * 100000000.0;
24 let base = (large - large.floor()) as f64;
25 min + base * (max - min)
26 }
27
28 pub fn chose<'a, T>(self: &Self, source: &'a [ T ]) -> &'a T {
30 let index = self.range(0 as f64, source.len() as f64) as usize;
31 &source[index]
32 }
33}