@@ -68,8 +68,10 @@ pub trait SliceRandom {
68
68
/// The element type.
69
69
type Item ;
70
70
71
- /// Returns a reference to one random element of the slice, or `None` if the
72
- /// slice is empty.
71
+ /// Uniformly sample one element
72
+ ///
73
+ /// Returns a reference to one uniformly-sampled random element of
74
+ /// the slice, or `None` if the slice is empty.
73
75
///
74
76
/// For slices, complexity is `O(1)`.
75
77
///
@@ -88,14 +90,18 @@ pub trait SliceRandom {
88
90
where
89
91
R : Rng + ?Sized ;
90
92
91
- /// Returns a mutable reference to one random element of the slice, or
92
- /// `None` if the slice is empty.
93
+ /// Uniformly sample one element (mut)
94
+ ///
95
+ /// Returns a mutable reference to one uniformly-sampled random element of
96
+ /// the slice, or `None` if the slice is empty.
93
97
///
94
98
/// For slices, complexity is `O(1)`.
95
99
fn choose_mut < R > ( & mut self , rng : & mut R ) -> Option < & mut Self :: Item >
96
100
where
97
101
R : Rng + ?Sized ;
98
102
103
+ /// Uniformly sample `amount` distinct elements
104
+ ///
99
105
/// Chooses `amount` elements from the slice at random, without repetition,
100
106
/// and in random order. The returned iterator is appropriate both for
101
107
/// collection into a `Vec` and filling an existing buffer (see example).
@@ -126,8 +132,10 @@ pub trait SliceRandom {
126
132
where
127
133
R : Rng + ?Sized ;
128
134
129
- /// Similar to [`choose`], but where the likelihood of each outcome may be
130
- /// specified.
135
+ /// Biased sampling for one element
136
+ ///
137
+ /// Returns a reference to one element of the slice, sampled according
138
+ /// to the provided weights. Returns `None` only if the slice is empty.
131
139
///
132
140
/// The specified function `weight` maps each item `x` to a relative
133
141
/// likelihood `weight(x)`. The probability of each item being selected is
@@ -168,8 +176,10 @@ pub trait SliceRandom {
168
176
+ Clone
169
177
+ Default ;
170
178
171
- /// Similar to [`choose_mut`], but where the likelihood of each outcome may
172
- /// be specified.
179
+ /// Biased sampling for one element (mut)
180
+ ///
181
+ /// Returns a mutable reference to one element of the slice, sampled according
182
+ /// to the provided weights. Returns `None` only if the slice is empty.
173
183
///
174
184
/// The specified function `weight` maps each item `x` to a relative
175
185
/// likelihood `weight(x)`. The probability of each item being selected is
@@ -199,6 +209,8 @@ pub trait SliceRandom {
199
209
+ Clone
200
210
+ Default ;
201
211
212
+ /// Biased sampling of `amount` distinct elements
213
+ ///
202
214
/// Similar to [`choose_multiple`], but where the likelihood of each element's
203
215
/// inclusion in the output may be specified. The elements are returned in an
204
216
/// arbitrary, unspecified order.
@@ -306,21 +318,28 @@ pub trait SliceRandom {
306
318
/// I am 😀!
307
319
/// ```
308
320
pub trait IteratorRandom : Iterator + Sized {
309
- /// Choose one element at random from the iterator.
321
+ /// Uniformly sample one element
310
322
///
311
- /// Returns `None` if and only if the iterator is empty.
323
+ /// Assuming that the [`Iterator::size_hint`] is correct, this method
324
+ /// returns one uniformly-sampled random element of the slice, or `None`
325
+ /// only if the slice is empty. Incorrect bounds on the `size_hint` may
326
+ /// cause this method to incorrectly return `None` if fewer elements than
327
+ /// the advertised `lower` bound are present and may prevent sampling of
328
+ /// elements beyond an advertised `upper` bound (i.e. incorrect `size_hint`
329
+ /// is memory-safe, but may result in unexpected `None` result and
330
+ /// non-uniform distribution).
312
331
///
313
- /// This method uses [`Iterator::size_hint`] for optimisation. With an
314
- /// accurate hint and where [`Iterator::nth`] is a constant-time operation
315
- /// this method can offer `O(1)` performance. Where no size hint is
332
+ /// With an accurate [`Iterator::size_hint`] and where [`Iterator::nth`] is
333
+ /// a constant-time operation, this method can offer `O(1)` performance.
334
+ /// Where no size hint is
316
335
/// available, complexity is `O(n)` where `n` is the iterator length.
317
336
/// Partial hints (where `lower > 0`) also improve performance.
318
337
///
319
- /// Note that the output values and the number of RNG samples used
320
- /// depends on size hints. In particular, `Iterator` combinators that don't
321
- /// change the values yielded but change the size hints may result in
322
- /// `choose` returning different elements. If you want consistent results
323
- /// and RNG usage consider using [`IteratorRandom::choose_stable`] .
338
+ /// Note further that [`Iterator::size_hint`] may affect the number of RNG
339
+ /// samples used as well as the result (while remaining uniform sampling).
340
+ /// Consider instead using [`IteratorRandom::choose_stable`] to avoid
341
+ /// [`Iterator`] combinators which only change size hints from affecting the
342
+ /// results .
324
343
fn choose < R > ( mut self , rng : & mut R ) -> Option < Self :: Item >
325
344
where
326
345
R : Rng + ?Sized ,
@@ -376,9 +395,7 @@ pub trait IteratorRandom: Iterator + Sized {
376
395
}
377
396
}
378
397
379
- /// Choose one element at random from the iterator.
380
- ///
381
- /// Returns `None` if and only if the iterator is empty.
398
+ /// Uniformly sample one element (stable)
382
399
///
383
400
/// This method is very similar to [`choose`] except that the result
384
401
/// only depends on the length of the iterator and the values produced by
@@ -437,6 +454,8 @@ pub trait IteratorRandom: Iterator + Sized {
437
454
}
438
455
}
439
456
457
+ /// Uniformly sample `amount` distinct elements into a buffer
458
+ ///
440
459
/// Collects values at random from the iterator into a supplied buffer
441
460
/// until that buffer is filled.
442
461
///
@@ -476,7 +495,7 @@ pub trait IteratorRandom: Iterator + Sized {
476
495
len
477
496
}
478
497
479
- /// Collects `amount` values at random from the iterator into a vector.
498
+ /// Uniformly sample `amount` distinct elements into a [`Vec`]
480
499
///
481
500
/// This is equivalent to `choose_multiple_fill` except for the result type.
482
501
///
0 commit comments