simple_rnd/
lib.rs

1pub struct Rand {
2    seed: u64,
3}
4impl Rand {
5    /// Creates instance of random number generator.
6    /// ```
7    /// use simple_rnd::Rand;
8    ///
9    /// let mut rand = Rand::new(0);
10    /// println!("{}", rand.next());
11    /// ```
12    pub fn new(seed: u64) -> Self {
13        Self { seed }
14    }
15    /// Generates the next random **u64** number.
16    /// ```
17    /// use simple_rnd::Rand;
18    ///
19    /// let mut rand = Rand::new(0);
20    /// println!("{}", rand.next());
21    /// ```
22    pub fn next(&mut self) -> u64 {
23        // Funny enough that all numbers end with 69.
24        self.seed = (self
25            .seed
26            .wrapping_mul(11546410263642718669)
27            .wrapping_add(1542007366999009369))
28        .wrapping_shr(6)
29            ^ self
30                .seed
31                .wrapping_mul(12994751319562203769)
32                .wrapping_add(992563119310360369)
33                .wrapping_shl(9);
34        self.seed
35    }
36    /// Mixes the seed with some u64 number.
37    /// ```
38    /// use simple_rnd::Rand;
39    ///
40    /// let mut rand = Rand::new(0);
41    /// 
42    /// println!("{}", rand.mix(228));
43    /// ```
44    pub fn mix(&mut self, other: u64) -> u64 {
45        // Funny enough that all numbers end with 69.
46        self.seed = (self
47            .seed
48            .wrapping_mul(11546410263642718669)
49            .wrapping_add(1542007366999009369))
50        .wrapping_shr(6)
51            ^ other
52                .wrapping_mul(12994751319562203769)
53                .wrapping_add(992563119310360369)
54                .wrapping_shl(9);
55        self.seed
56    }
57}