Skip to content

Commit 0c28eba

Browse files
authored
Improve doc of slice and iterator "choose" methods (#1343)
2 parents f3dd0b8 + e609fa9 commit 0c28eba

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ jobs:
113113
matrix:
114114
include:
115115
- os: ubuntu-latest
116-
target: mips-unknown-linux-gnu
116+
target: powerpc-unknown-linux-gnu
117117
toolchain: stable
118118

119119
steps:

src/seq/mod.rs

+41-22
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ pub trait SliceRandom {
6868
/// The element type.
6969
type Item;
7070

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.
7375
///
7476
/// For slices, complexity is `O(1)`.
7577
///
@@ -88,14 +90,18 @@ pub trait SliceRandom {
8890
where
8991
R: Rng + ?Sized;
9092

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.
9397
///
9498
/// For slices, complexity is `O(1)`.
9599
fn choose_mut<R>(&mut self, rng: &mut R) -> Option<&mut Self::Item>
96100
where
97101
R: Rng + ?Sized;
98102

103+
/// Uniformly sample `amount` distinct elements
104+
///
99105
/// Chooses `amount` elements from the slice at random, without repetition,
100106
/// and in random order. The returned iterator is appropriate both for
101107
/// collection into a `Vec` and filling an existing buffer (see example).
@@ -126,8 +132,10 @@ pub trait SliceRandom {
126132
where
127133
R: Rng + ?Sized;
128134

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.
131139
///
132140
/// The specified function `weight` maps each item `x` to a relative
133141
/// likelihood `weight(x)`. The probability of each item being selected is
@@ -168,8 +176,10 @@ pub trait SliceRandom {
168176
+ Clone
169177
+ Default;
170178

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.
173183
///
174184
/// The specified function `weight` maps each item `x` to a relative
175185
/// likelihood `weight(x)`. The probability of each item being selected is
@@ -199,6 +209,8 @@ pub trait SliceRandom {
199209
+ Clone
200210
+ Default;
201211

212+
/// Biased sampling of `amount` distinct elements
213+
///
202214
/// Similar to [`choose_multiple`], but where the likelihood of each element's
203215
/// inclusion in the output may be specified. The elements are returned in an
204216
/// arbitrary, unspecified order.
@@ -306,21 +318,28 @@ pub trait SliceRandom {
306318
/// I am 😀!
307319
/// ```
308320
pub trait IteratorRandom: Iterator + Sized {
309-
/// Choose one element at random from the iterator.
321+
/// Uniformly sample one element
310322
///
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).
312331
///
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
316335
/// available, complexity is `O(n)` where `n` is the iterator length.
317336
/// Partial hints (where `lower > 0`) also improve performance.
318337
///
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.
324343
fn choose<R>(mut self, rng: &mut R) -> Option<Self::Item>
325344
where
326345
R: Rng + ?Sized,
@@ -376,9 +395,7 @@ pub trait IteratorRandom: Iterator + Sized {
376395
}
377396
}
378397

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)
382399
///
383400
/// This method is very similar to [`choose`] except that the result
384401
/// only depends on the length of the iterator and the values produced by
@@ -437,6 +454,8 @@ pub trait IteratorRandom: Iterator + Sized {
437454
}
438455
}
439456

457+
/// Uniformly sample `amount` distinct elements into a buffer
458+
///
440459
/// Collects values at random from the iterator into a supplied buffer
441460
/// until that buffer is filled.
442461
///
@@ -476,7 +495,7 @@ pub trait IteratorRandom: Iterator + Sized {
476495
len
477496
}
478497

479-
/// Collects `amount` values at random from the iterator into a vector.
498+
/// Uniformly sample `amount` distinct elements into a [`Vec`]
480499
///
481500
/// This is equivalent to `choose_multiple_fill` except for the result type.
482501
///

0 commit comments

Comments
 (0)