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
// External includes.
use rand::distributions::Distribution;
use rand::{thread_rng, Rng};
// Standard includes.
// Internal includes.
use super::{Count, ProvidesCount};
/// Provides a range of [`Count`](type.Count.html)s, from a minimum count to a maximum count.
///
/// Both of these methods provide a random count between the minimum count in the range, and the maximum count in the range.
/// ```
/// # use dungen_minion_geometry::*;
/// use std::sync::Arc;
/// use rayon::prelude::*;
///
/// use rand::{thread_rng, Rng};
/// // The maximum count is an inclusive bound.
/// let count_range = Arc::new(CountRange::new(4, 13));
/// // Random generators are hard to guarantee. But this should be viable.
/// [0..5_000].par_iter().for_each(|_i| {
/// let rand_count = count_range.provide_count();
/// assert!(rand_count >= 4 && rand_count <= 13);
/// });
///
/// [0..5_000].par_iter().for_each(|_i| {
/// let rand_count = thread_rng().sample(*count_range);
/// assert!(rand_count >= 4 && rand_count <= 13);
/// });
/// ```
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub struct CountRange {
min_count: Count,
max_count: Count,
}
impl CountRange {
/// Creates a new `SizeRange` from a minimum `Size` and a maximum `Size`.
#[must_use]
pub fn new(min_count: Count, max_count: Count) -> Self {
Self {
min_count,
max_count,
}
}
}
impl Distribution<Count> for CountRange {
fn sample<R>(&self, rng: &mut R) -> Count
where
R: Rng + ?Sized,
{
rng.gen_range(self.min_count..=self.max_count)
}
}
impl ProvidesCount for CountRange {
fn provide_count(&self) -> Count {
self.sample(&mut thread_rng())
}
}