random_number/
random_fill_functions.rs

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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
use std::{
    cmp::Ordering,
    ops::{Bound, RangeBounds},
};

use crate::{
    rand::{
        distributions::{
            uniform::{SampleBorrow, SampleUniform, Uniform},
            Distribution,
        },
        thread_rng, Rng,
    },
    Bounded,
};

/// Generate random values in the range [`min`, `max_exclusive`) with a new lazily-initialized thread-local random number generator.
///
/// Panics if `min >= max_exclusive`.
#[inline]
pub fn random_fill_exclusively<X: SampleUniform, B1: SampleBorrow<X>, B2: SampleBorrow<X>>(
    out: &mut [X],
    min: B1,
    max_exclusive: B2,
) {
    random_fill_exclusively_with_rng(out, min, max_exclusive, &mut thread_rng())
}

/// Generate random values in the range [`min`, `max_exclusive`) with an existing random number generator.
///
/// Panics if `min >= max_exclusive`.
#[inline]
pub fn random_fill_exclusively_with_rng<
    X: SampleUniform,
    B1: SampleBorrow<X>,
    B2: SampleBorrow<X>,
    T: Rng,
>(
    out: &mut [X],
    min: B1,
    max_exclusive: B2,
    rng: &mut T,
) {
    let uniform = Uniform::new(min, max_exclusive);

    for x in out.iter_mut() {
        *x = uniform.sample(rng);
    }
}

/// Generate random values in the range [`min`, `max_inclusive`] with a new lazily-initialized thread-local random number generator.
///
/// Panics if `min > max_inclusive`.
#[inline]
pub fn random_fill_inclusively<X: SampleUniform, B1: SampleBorrow<X>, B2: SampleBorrow<X>>(
    out: &mut [X],
    min: B1,
    max_inclusive: B2,
) {
    random_fill_inclusively_with_rng(out, min, max_inclusive, &mut thread_rng());
}

/// Generate random values in the range [`min`, `max_inclusive`] with an existing random number generator.
///
/// Panics if `min > max_inclusive`.
#[inline]
pub fn random_fill_inclusively_with_rng<
    X: SampleUniform,
    B1: SampleBorrow<X>,
    B2: SampleBorrow<X>,
    T: Rng,
>(
    out: &mut [X],
    min: B1,
    max_inclusive: B2,
    rng: &mut T,
) {
    let uniform = Uniform::new_inclusive(min, max_inclusive);
    let mut random_iter = uniform.sample_iter(rng);

    for x in out.iter_mut() {
        *x = random_iter.next().unwrap();
    }
}

/// Generate random values in the range [`a`, `b`] or [`b`, `a`] with a new lazily-initialized thread-local random number generator.
///
/// Panics if a and b can not be compared.
#[inline]
pub fn random_fill_inclusively_cmp<X: SampleUniform + Ord + Clone, B: SampleBorrow<X>>(
    out: &mut [X],
    a: B,
    b: B,
) {
    random_fill_inclusively_cmp_with_rng(out, a, b, &mut thread_rng());
}

/// Generate random values in the range [`a`, `b`] or [`b`, `a`] with an existing random number generator.
#[inline]
pub fn random_fill_inclusively_cmp_with_rng<
    X: SampleUniform + Ord + Clone,
    B: SampleBorrow<X>,
    T: Rng,
>(
    out: &mut [X],
    a: B,
    b: B,
    rng: &mut T,
) {
    match a.borrow().cmp(b.borrow()) {
        Ordering::Greater => random_fill_inclusively_with_rng(out, b, a, rng),
        Ordering::Equal => {
            for x in out.iter_mut() {
                *x = a.borrow().clone();
            }
        },
        Ordering::Less => random_fill_inclusively_with_rng(out, a, b, rng),
    }
}

/// Generate random values in the range of the output type with a new lazily-initialized thread-local random number generator.
#[inline]
pub fn random_fill<X: SampleUniform + Bounded>(out: &mut [X]) {
    random_fill_with_rng(out, &mut thread_rng())
}

/// Generate random values in the range of the output type with an existing random number generator.
#[inline]
pub fn random_fill_with_rng<X: SampleUniform + Bounded, T: Rng>(out: &mut [X], rng: &mut T) {
    random_fill_inclusively_with_rng(out, X::min_value(), X::max_value(), rng);
}

/// Generate random values in the range [`min`, `Bounded::max_value()`] with a new lazily-initialized thread-local random number generator.
#[inline]
pub fn random_fill_at_least<X: SampleUniform + Bounded, B: SampleBorrow<X>>(out: &mut [X], min: B) {
    random_fill_at_least_with_rng(out, min, &mut thread_rng());
}

/// Generate random values in the range [`min`, `X::max_value()`] with an existing random number generator.
#[inline]
pub fn random_fill_at_least_with_rng<X: SampleUniform + Bounded, B: SampleBorrow<X>, T: Rng>(
    out: &mut [X],
    min: B,
    rng: &mut T,
) {
    random_fill_inclusively_with_rng(out, min, X::max_value(), rng);
}

/// Generate random values in the range [`X::min_value()`, `max_inclusive`] with a new lazily-initialized thread-local random number generator.
#[inline]
pub fn random_fill_at_most<X: SampleUniform + Bounded, B: SampleBorrow<X>>(
    out: &mut [X],
    max_inclusive: B,
) {
    random_fill_at_most_with_rng(out, max_inclusive, &mut thread_rng());
}

/// Generate random values in the range [`X::min_value()`, `max_inclusive`] with an existing random number generator.
#[inline]
pub fn random_fill_at_most_with_rng<X: SampleUniform + Bounded, B: SampleBorrow<X>, T: Rng>(
    out: &mut [X],
    max_inclusive: B,
    rng: &mut T,
) {
    random_fill_inclusively_with_rng(out, X::min_value(), max_inclusive, rng);
}

/// Generate random values in the range [`X::min_value()`, `max_exclusive`) with a new lazily-initialized thread-local random number generator.
///
/// Panics if X::min_value() == max_exclusive.
#[inline]
pub fn random_fill_at_most_exclusively<X: SampleUniform + Bounded, B: SampleBorrow<X>>(
    out: &mut [X],
    max_exclusive: B,
) {
    random_fill_at_most_exclusively_with_rng(out, max_exclusive, &mut thread_rng());
}

/// Generate random values in the range [`X::min_value()`, `max_exclusive`) with an existing random number generator.
///
/// Panics if X::min_value() == max_exclusive.
#[inline]
pub fn random_fill_at_most_exclusively_with_rng<
    X: SampleUniform + Bounded,
    B: SampleBorrow<X>,
    T: Rng,
>(
    out: &mut [X],
    max_exclusive: B,
    rng: &mut T,
) {
    random_fill_exclusively_with_rng(out, X::min_value(), max_exclusive, rng);
}

/// Generate random values in a specific range with a new lazily-initialized thread-local random number generator.
///
/// Panics if the start bound is exclusive.
#[inline]
pub fn random_fill_ranged<X: SampleUniform + Bounded, R: RangeBounds<X>>(out: &mut [X], range: R) {
    random_fill_ranged_with_rng(out, range, &mut thread_rng())
}

/// Generate random values in a specific range with an existing random number generator.
///
/// Panics if the start bound is exclusive.
#[inline]
pub fn random_fill_ranged_with_rng<X: SampleUniform + Bounded, R: RangeBounds<X>, T: Rng>(
    out: &mut [X],
    range: R,
    rng: &mut T,
) {
    let start = range.start_bound();
    let end = range.end_bound();

    match start {
        Bound::Excluded(_) => {
            panic!("random_fill_ranged_with_rng called with a start bound which is exclusive")
        },
        Bound::Included(min) => match end {
            Bound::Excluded(max_exclusive) => {
                random_fill_exclusively_with_rng(out, min, max_exclusive, rng)
            },
            Bound::Included(max_inclusive) => {
                random_fill_inclusively_with_rng(out, min, max_inclusive, rng)
            },
            Bound::Unbounded => random_fill_at_least_with_rng(out, min, rng),
        },
        Bound::Unbounded => match end {
            Bound::Excluded(max_exclusive) => {
                random_fill_at_most_exclusively_with_rng(out, max_exclusive, rng)
            },
            Bound::Included(max_inclusive) => random_fill_at_most_with_rng(out, max_inclusive, rng),
            Bound::Unbounded => random_fill_with_rng(out, rng),
        },
    }
}