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
use super::rand::base::system::{get_seed, refresh_seed};
use super::rand::Solver;

pub trait Rand {
    fn rand() -> Self;
}

macro_rules! impl_rand_trait {
    ($($t: ty)*) => (
        $(
            impl Rand for $t {
                fn rand() -> Self {
                    let mut solver = Solver::new();
                    let mut _base = solver.get_base(0).unwrap();
                    _base.srand(get_seed());
                    solver.set_base(0, _base);
                    let _return = solver.rand();
                    refresh_seed(solver.get_base(0).unwrap().get_seed());                   
                    _return as $t
                }
            }
        ) *
    )
}

impl_rand_trait!{u8 u16 u32 u64 usize}
impl_rand_trait!{i8 i16 i32 i64 isize}
impl_rand_trait!{f32 f64}

impl Rand for char {
    fn rand() -> Self {
        (u8::rand() % 128) as char
    }
}

impl Rand for String {
    fn rand() -> Self {
        let mut result = "".to_string();
        loop {
            let _char = char::rand();
            if _char == '\0' {
                return result;
            }
            result += &_char.to_string();
        }
    }
}