Skip to content

Commit 34b2e45

Browse files
authored
Rename fns choose_multiple* → sample*; add choose*_iter (#1632)
2 parents 052b80f + 198475a commit 34b2e45

File tree

5 files changed

+167
-58
lines changed

5 files changed

+167
-58
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
1010

1111
## [0.10.0 — Unreleased]
1212
### Changes
13+
- Rename fns `IndexedRandom::choose_multiple` -> `sample`, `choose_multiple_array` -> `sample_array`, `choose_multiple_weighted` -> `sample_weighted`, struct `SliceChooseIter` -> `IndexedSamples` and fns `IteratorRandom::choose_multiple` -> `sample`, `choose_multiple_fill` -> `sample_fill` (#1632)
1314
- Use Edition 2024 and MSRV 1.85 (#1653)
1415
- Let `Fill` be implemented for element types, not sliceable types (#1652)
1516

1617
### Additions
18+
- Add fns `IndexedRandom::choose_iter`, `choose_weighted_iter` (#1632)
1719
- Pub export `Xoshiro128PlusPlus`, `Xoshiro256PlusPlus` prngs (#1649)
1820

1921
## [0.9.2 — 2025-07-20]

benches/benches/seq_choose.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn bench(c: &mut Criterion) {
3030

3131
let lens = [(1, 1000), (950, 1000), (10, 100), (90, 100)];
3232
for (amount, len) in lens {
33-
let name = format!("seq_slice_choose_multiple_{amount}_of_{len}");
33+
let name = format!("seq_slice_sample_{amount}_of_{len}");
3434
c.bench_function(name.as_str(), |b| {
3535
let mut rng = Pcg32::from_rng(&mut rand::rng());
3636
let mut buf = [0i32; 1000];
@@ -44,7 +44,7 @@ pub fn bench(c: &mut Criterion) {
4444
b.iter(|| {
4545
// Collect full result to prevent unwanted shortcuts getting
4646
// first element (in case sample_indices returns an iterator).
47-
for (slot, sample) in y.iter_mut().zip(x.choose_multiple(&mut rng, amount)) {
47+
for (slot, sample) in y.iter_mut().zip(x.sample(&mut rng, amount)) {
4848
*slot = *sample;
4949
}
5050
y[amount - 1]
@@ -54,7 +54,7 @@ pub fn bench(c: &mut Criterion) {
5454

5555
let lens = [(1, 1000), (950, 1000), (10, 100), (90, 100)];
5656
for (amount, len) in lens {
57-
let name = format!("seq_slice_choose_multiple_weighted_{amount}_of_{len}");
57+
let name = format!("seq_slice_sample_weighted_{amount}_of_{len}");
5858
c.bench_function(name.as_str(), |b| {
5959
let mut rng = Pcg32::from_rng(&mut rand::rng());
6060
let mut buf = [0i32; 1000];
@@ -68,7 +68,7 @@ pub fn bench(c: &mut Criterion) {
6868
b.iter(|| {
6969
// Collect full result to prevent unwanted shortcuts getting
7070
// first element (in case sample_indices returns an iterator).
71-
let samples_iter = x.choose_multiple_weighted(&mut rng, amount, |_| 1.0).unwrap();
71+
let samples_iter = x.sample_weighted(&mut rng, amount, |_| 1.0).unwrap();
7272
for (slot, sample) in y.iter_mut().zip(samples_iter) {
7373
*slot = *sample;
7474
}
@@ -77,21 +77,21 @@ pub fn bench(c: &mut Criterion) {
7777
});
7878
}
7979

80-
c.bench_function("seq_iter_choose_multiple_10_of_100", |b| {
80+
c.bench_function("seq_iter_sample_10_of_100", |b| {
8181
let mut rng = Pcg32::from_rng(&mut rand::rng());
8282
let mut buf = [0i32; 100];
8383
rng.fill(&mut buf);
8484
let x = black_box(&buf);
85-
b.iter(|| x.iter().cloned().choose_multiple(&mut rng, 10))
85+
b.iter(|| x.iter().cloned().sample(&mut rng, 10))
8686
});
8787

88-
c.bench_function("seq_iter_choose_multiple_fill_10_of_100", |b| {
88+
c.bench_function("seq_iter_sample_fill_10_of_100", |b| {
8989
let mut rng = Pcg32::from_rng(&mut rand::rng());
9090
let mut buf = [0i32; 100];
9191
rng.fill(&mut buf);
9292
let x = black_box(&buf);
9393
let mut buf = [0; 10];
94-
b.iter(|| x.iter().cloned().choose_multiple_fill(&mut rng, &mut buf))
94+
b.iter(|| x.iter().cloned().sample_fill(&mut rng, &mut buf))
9595
});
9696

9797
bench_rng::<rand_chacha::ChaCha20Rng>(c, "ChaCha20");

src/seq/iterator.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ pub trait IteratorRandom: Iterator + Sized {
199199
/// case this equals the number of elements available.
200200
///
201201
/// Complexity is `O(n)` where `n` is the length of the iterator.
202-
/// For slices, prefer [`IndexedRandom::choose_multiple`].
203-
fn choose_multiple_fill<R>(mut self, rng: &mut R, buf: &mut [Self::Item]) -> usize
202+
/// For slices, prefer [`IndexedRandom::sample`].
203+
fn sample_fill<R>(mut self, rng: &mut R, buf: &mut [Self::Item]) -> usize
204204
where
205205
R: Rng + ?Sized,
206206
{
@@ -228,7 +228,7 @@ pub trait IteratorRandom: Iterator + Sized {
228228

229229
/// Uniformly sample `amount` distinct elements into a [`Vec`]
230230
///
231-
/// This is equivalent to `choose_multiple_fill` except for the result type.
231+
/// This is equivalent to `sample_fill` except for the result type.
232232
///
233233
/// Although the elements are selected randomly, the order of elements in
234234
/// the buffer is neither stable nor fully random. If random ordering is
@@ -239,9 +239,9 @@ pub trait IteratorRandom: Iterator + Sized {
239239
/// elements available.
240240
///
241241
/// Complexity is `O(n)` where `n` is the length of the iterator.
242-
/// For slices, prefer [`IndexedRandom::choose_multiple`].
242+
/// For slices, prefer [`IndexedRandom::sample`].
243243
#[cfg(feature = "alloc")]
244-
fn choose_multiple<R>(mut self, rng: &mut R, amount: usize) -> Vec<Self::Item>
244+
fn sample<R>(mut self, rng: &mut R, amount: usize) -> Vec<Self::Item>
245245
where
246246
R: Rng + ?Sized,
247247
{
@@ -266,6 +266,25 @@ pub trait IteratorRandom: Iterator + Sized {
266266
}
267267
reservoir
268268
}
269+
270+
/// Deprecated: use [`Self::sample_fill`] instead
271+
#[deprecated(since = "0.9.2", note = "Renamed to `sample_fill`")]
272+
fn choose_multiple_fill<R>(self, rng: &mut R, buf: &mut [Self::Item]) -> usize
273+
where
274+
R: Rng + ?Sized,
275+
{
276+
self.sample_fill(rng, buf)
277+
}
278+
279+
/// Deprecated: use [`Self::sample`] instead
280+
#[cfg(feature = "alloc")]
281+
#[deprecated(since = "0.9.2", note = "Renamed to `sample`")]
282+
fn choose_multiple<R>(self, rng: &mut R, amount: usize) -> Vec<Self::Item>
283+
where
284+
R: Rng + ?Sized,
285+
{
286+
self.sample(rng, amount)
287+
}
269288
}
270289

271290
impl<I> IteratorRandom for I where I: Iterator + Sized {}
@@ -542,8 +561,8 @@ mod test {
542561

543562
let mut r = crate::test::rng(401);
544563
let vals = (min_val..max_val).collect::<Vec<i32>>();
545-
let small_sample = vals.iter().choose_multiple(&mut r, 5);
546-
let large_sample = vals.iter().choose_multiple(&mut r, vals.len() + 5);
564+
let small_sample = vals.iter().sample(&mut r, 5);
565+
let large_sample = vals.iter().sample(&mut r, vals.len() + 5);
547566

548567
assert_eq!(small_sample.len(), 5);
549568
assert_eq!(large_sample.len(), vals.len());
@@ -650,20 +669,17 @@ mod test {
650669
}
651670

652671
#[test]
653-
fn value_stability_choose_multiple() {
672+
fn value_stability_sample() {
654673
fn do_test<I: Clone + Iterator<Item = u32>>(iter: I, v: &[u32]) {
655674
let mut rng = crate::test::rng(412);
656675
let mut buf = [0u32; 8];
657-
assert_eq!(
658-
iter.clone().choose_multiple_fill(&mut rng, &mut buf),
659-
v.len()
660-
);
676+
assert_eq!(iter.clone().sample_fill(&mut rng, &mut buf), v.len());
661677
assert_eq!(&buf[0..v.len()], v);
662678

663679
#[cfg(feature = "alloc")]
664680
{
665681
let mut rng = crate::test::rng(412);
666-
assert_eq!(iter.choose_multiple(&mut rng, v.len()), v);
682+
assert_eq!(iter.sample(&mut rng, v.len()), v);
667683
}
668684
}
669685

src/seq/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ mod index_;
4040
pub use crate::distr::weighted::Error as WeightError;
4141
pub use iterator::IteratorRandom;
4242
#[cfg(feature = "alloc")]
43+
pub use slice::IndexedSamples;
44+
#[allow(deprecated)]
45+
#[cfg(feature = "alloc")]
4346
pub use slice::SliceChooseIter;
4447
pub use slice::{IndexedMutRandom, IndexedRandom, SliceRandom};
4548

0 commit comments

Comments
 (0)