summaryrefslogtreecommitdiff
path: root/2026/games/asrael_io/source/src/rng.rs
blob: 6e46de0d97f3c45c5bcbd36da1b16a951912d146 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
pub struct Rng(u32);

impl Rng {
    pub fn new(seed: u32) -> Self {
        let mut rng = Self(seed.max(1));
        rng.next();
        rng
    }

    pub fn next(&mut self) -> u32 {
        self.0 ^= self.0 << 13;
        self.0 ^= self.0 >> 17;
        self.0 ^= self.0 << 5;

        self.0
    }

    pub fn range(&mut self, n: u32) -> u32 {
        self.next() % n
    }

    pub fn f32(&mut self) -> f32 {
        self.next() as f32 / u32::MAX as f32
    }

    pub fn chance(&mut self, p: f32) -> bool {
        self.f32() < p
    }
}

impl Default for Rng {
    fn default() -> Self {
        Self::new(u32::from_be_bytes(*b"CLNK"))
    }
}