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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use std::cell::Cell;
use std::ops::Bound;
use std::ops::RangeBounds;

pub struct RandState {
    pub seed: Cell<u64>,
}

impl RandState {
    /// Constructs a new instance of [RandState] with stored `seed`.
    pub fn new(seed: u64) -> Self {
        Self { seed: Cell::new(seed) }
    }
}

thread_local! {
     static SEED: RandState = RandState::new(common::time::get_unix_timestamp())
}

macro_rules! rand_definition {
    ($type:ident, $min_value:expr, $max_value:expr) => {
        /// Gets a random number within `range`.
        pub fn $type(range: impl RangeBounds<$type>) -> $type {
            let from = match range.start_bound() {
                Bound::Included(v) => *v,
                Bound::Excluded(v) => *v + 1,
                Bound::Unbounded => $min_value,
            };

            let to = match range.end_bound() {
                Bound::Included(v) => *v,
                Bound::Excluded(v) => *v - 1,
                Bound::Unbounded => $max_value,
            };

            SEED.with(|state| {
                let (value, seed) = rand(state.seed.get());
                let result = if from == $min_value && to == $max_value {
                    value as $type
                } else {
                    (value % (((to as i128) - (from as i128) + 1) as u64)) as $type + from
                };

                state.seed.set(seed);
                result
            })
        }
    };
}

rand_definition!(i8, i8::MIN, i8::MAX);
rand_definition!(u8, u8::MIN, u8::MAX);
rand_definition!(i16, i16::MIN, i16::MAX);
rand_definition!(u16, u16::MIN, u16::MAX);
rand_definition!(i32, i32::MIN, i32::MAX);
rand_definition!(u32, u32::MIN, u32::MAX);
rand_definition!(i64, i64::MIN, i64::MAX);
rand_definition!(u64, u64::MIN, u64::MAX);
rand_definition!(isize, isize::MIN, isize::MAX);
rand_definition!(usize, usize::MIN, usize::MAX);

/// Sets an initial seed for LCG.
pub fn seed(seed: u64) {
    SEED.with(|state| {
        state.seed.set(seed);
    });
}

/// https://en.wikipedia.org/wiki/Xorshift#xorshift*
pub const fn rand(seed: u64) -> (u64, u64) {
    let mut x = seed;
    x ^= x >> 12;
    x ^= x << 25;
    x ^= x >> 27;

    (x.wrapping_mul(0x2545f4914f6cdd1d), x)
}