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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use crate::Rng;
use core::ops::{Bound, RangeBounds};
macro_rules! gen {
($($type:ty),+) => {
$(
impl<Generator: Rng<OUTPUT>, const OUTPUT: usize> RandomGen<Generator, OUTPUT> for $type {
fn random(rng: &mut Generator) -> Self {
let mut bytes = [0u8; core::mem::size_of::<$type>()];
rng.fill_bytes(&mut bytes);
Self::from_ne_bytes(bytes)
}
}
)+
};
}
macro_rules! range {
($(($type:ty, $bigger:ty, $signed:ty)),+) => {
$(
impl<Generator: Rng<OUTPUT>, const OUTPUT: usize> RandomRange<Generator, OUTPUT> for $type {
fn random_range<Bounds: RangeBounds<Self>>(rng: &mut Generator, bounds: Bounds) -> Self {
const BITS: $bigger = core::mem::size_of::<$type>() as $bigger * 8;
let lower = match bounds.start_bound() {
Bound::Included(lower) => *lower,
Bound::Excluded(lower) => lower.saturating_add(1),
Bound::Unbounded => <$type>::MIN,
};
let upper = match bounds.end_bound() {
Bound::Included(upper) => upper.saturating_add(1),
Bound::Excluded(upper) => *upper,
Bound::Unbounded => <$type>::MAX,
};
assert!(upper >= lower, "{} >= {} (lower bound was bigger than upper bound)", upper, lower);
let upper = upper.saturating_sub(lower);
let mut value = Self::random(rng);
let mut m = (upper as $bigger).wrapping_mul(value as $bigger);
if (m as $type) < upper {
let t = (!upper + 1) % upper;
while (m as $type) < t {
value = Self::random(rng);
m = (upper as $bigger).wrapping_mul(value as $bigger);
}
}
(m >> BITS) as $type + lower
}
}
impl<Generator: Rng<OUTPUT>, const OUTPUT: usize> RandomRange<Generator, OUTPUT> for $signed {
fn random_range<Bounds: RangeBounds<Self>>(r: &mut Generator, bounds: Bounds) -> Self {
let lower = match bounds.start_bound() {
Bound::Included(lower) => *lower,
Bound::Excluded(lower) => lower.saturating_add(1),
Bound::Unbounded => <$signed>::MIN
};
let upper = match bounds.end_bound() {
Bound::Included(upper) => *upper,
Bound::Excluded(upper) => upper.saturating_sub(1),
Bound::Unbounded => <$signed>::MAX,
};
assert!(upper >= lower, "{} >= {} (lower bound was bigger than upper bound)", upper, lower);
let lower = lower.wrapping_sub(<$signed>::MIN) as $type;
let upper = upper.wrapping_sub(<$signed>::MIN) as $type;
<$type>::random_range(r, lower..=upper).wrapping_add(<$signed>::MAX as $type) as $signed
}
}
)+
}
}
pub trait RandomGen<Generator: Rng<OUTPUT>, const OUTPUT: usize> {
fn random(rng: &mut Generator) -> Self;
}
pub trait RandomRange<Generator: Rng<OUTPUT>, const OUTPUT: usize>:
RandomGen<Generator, OUTPUT>
{
fn random_range<Bounds: RangeBounds<Self>>(nng: &mut Generator, range: Bounds) -> Self;
}
impl<Generator: Rng<OUTPUT>, const OUTPUT: usize> RandomGen<Generator, OUTPUT> for bool {
fn random(rng: &mut Generator) -> Self {
u8::random(rng) < 0b10000000
}
}
impl<Generator: Rng<OUTPUT>, const OUTPUT: usize> RandomGen<Generator, OUTPUT> for f32 {
fn random(rng: &mut Generator) -> Self {
(u32::random(rng) as f32) / (u32::MAX as f32)
}
}
impl<Generator: Rng<OUTPUT>, const OUTPUT: usize> RandomGen<Generator, OUTPUT> for f64 {
fn random(rng: &mut Generator) -> Self {
(u64::random(rng) as f64) / (u64::MAX as f64)
}
}
gen!(i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize);
range!(
(u8, u16, i8),
(u16, u32, i16),
(u32, u64, i32),
(u64, u128, i64)
);
#[cfg(target_pointer_width = "16")]
range!((usize, u32, isize));
#[cfg(target_pointer_width = "32")]
range!((usize, u64, isize));
#[cfg(target_pointer_width = "64")]
range!((usize, u128, isize));
#[cfg(test)]
mod tests {
use crate::{Rng, WyRand};
#[test]
fn ensure_unsigned_in_range() {
let mut rng = WyRand::new();
for _ in 0..1000 {
let number = rng.generate_range(10_u64..=20);
assert!(
(10..=20).contains(&number),
"{} was outside of 10..=20",
number
);
let number = rng.generate_range(10_u64..30);
assert!(
(10..30).contains(&number),
"{} was outside of 10..30",
number
);
let number = rng.generate_range(512_u64..);
assert!((512..).contains(&number), "{} was outside of 512..", number);
let number = rng.generate_range(..1024_u64);
assert!(
(..1024).contains(&number),
"{} was outside of ..1024",
number
);
}
}
#[test]
fn ensure_signed_in_range() {
let mut rng = WyRand::new();
for _ in 0..1000 {
let number = rng.generate_range(-50..);
assert!((-50..).contains(&number), "{} was outside of -50..", number);
let number = rng.generate_range(..512);
assert!((..512).contains(&number), "{} was outside of ..512", number);
let number = rng.generate_range(..-32);
assert!((..-32).contains(&number), "{} was outside of ..-32", number);
}
}
#[test]
fn ensure_floats_generate_properly() {
let mut rng = WyRand::new();
for _ in 0..1000 {
let number = rng.generate::<f32>();
assert!(1.0 >= number, "{} was bigger than 1.0", number);
assert!(number >= 0.0, "0 was bigger than {}", number);
let number = rng.generate::<f64>();
assert!(1.0 >= number, "{} was bigger than 1.0", number);
assert!(number >= 0.0, "0 was bigger than {}", number);
}
}
#[test]
#[should_panic]
fn ensure_invalid_range_panics() {
let mut rng = WyRand::new();
#[allow(clippy::reversed_empty_ranges)]
rng.generate_range(10..=5);
}
}