smartcore/
rand_custom.rs

1#[cfg(not(feature = "std_rand"))]
2pub use rand::rngs::SmallRng as RngImpl;
3#[cfg(feature = "std_rand")]
4pub use rand::rngs::StdRng as RngImpl;
5use rand::SeedableRng;
6
7/// Custom switch for random fuctions
8pub fn get_rng_impl(seed: Option<u64>) -> RngImpl {
9    match seed {
10        Some(seed) => RngImpl::seed_from_u64(seed),
11        None => {
12            cfg_if::cfg_if! {
13                if #[cfg(feature = "std_rand")] {
14                    use rand::RngCore;
15                    RngImpl::seed_from_u64(rand::thread_rng().next_u64())
16                } else {
17                    // no std_random feature build, use getrandom
18                    #[cfg(feature = "js")]
19                    {
20                        let mut buf = [0u8; 64];
21                        getrandom::getrandom(&mut buf).unwrap();
22                        RngImpl::seed_from_u64(buf[0] as u64)
23                    }
24                    #[cfg(not(feature = "js"))]
25                    {
26                        // Using 0 as default seed
27                        RngImpl::seed_from_u64(0)
28                    }
29                }
30            }
31        }
32    }
33}