random_constructible/
prim_traits.rs

1crate::ix!();
2
3// Macro to implement RandConstruct for floating-point types
4macro_rules! impl_rand_construct_for_float {
5    ($($t:ty),*) => {
6        $(
7            impl RandConstruct for $t {
8                fn random() -> Self {
9                    rand::random::<$t>()
10                }
11                fn uniform() -> Self {
12                    let mut rng = rand::thread_rng();
13                    rng.gen_range(0.0 as $t .. 1.0 as $t)
14                }
15                fn random_with_rng<R: Rng + ?Sized>(rng: &mut R) -> Self {
16                    rng.gen::<$t>()
17                }
18            }
19        )*
20    };
21}
22
23impl_rand_construct_for_float!{f32, f64}
24
25// Macro to implement RandConstruct for integer types
26macro_rules! impl_rand_construct_for_integer {
27    ($($t:ty),*) => {
28        $(
29            impl RandConstruct for $t {
30                fn random() -> Self {
31                    rand::random::<$t>()
32                }
33                fn uniform() -> Self {
34                    let mut rng = rand::thread_rng();
35                    rng.gen_range(<$t>::MIN..=<$t>::MAX)
36                }
37                fn random_with_rng<R: Rng + ?Sized>(rng: &mut R) -> Self {
38                    rng.gen::<$t>()
39                }
40            }
41        )*
42    };
43}
44
45impl_rand_construct_for_integer!{i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize}