@@ -78,16 +78,16 @@ pub trait IndexedRandom: Index<usize> {
78
78
/// let sample = "Hello, audience!".as_bytes();
79
79
///
80
80
/// // collect the results into a vector:
81
- /// let v: Vec<u8> = sample.choose_multiple (&mut rng, 3).cloned().collect();
81
+ /// let v: Vec<u8> = sample.sample (&mut rng, 3).cloned().collect();
82
82
///
83
83
/// // store in a buffer:
84
84
/// let mut buf = [0u8; 5];
85
- /// for (b, slot) in sample.choose_multiple (&mut rng, buf.len()).zip(buf.iter_mut()) {
85
+ /// for (b, slot) in sample.sample (&mut rng, buf.len()).zip(buf.iter_mut()) {
86
86
/// *slot = *b;
87
87
/// }
88
88
/// ```
89
89
#[ cfg( feature = "alloc" ) ]
90
- fn choose_multiple < R > ( & self , rng : & mut R , amount : usize ) -> SliceChooseIter < Self , Self :: Output >
90
+ fn sample < R > ( & self , rng : & mut R , amount : usize ) -> SliceChooseIter < Self , Self :: Output >
91
91
where
92
92
Self :: Output : Sized ,
93
93
R : Rng + ?Sized ,
@@ -114,9 +114,9 @@ pub trait IndexedRandom: Index<usize> {
114
114
/// let mut rng = &mut rand::rng();
115
115
/// let sample = "Hello, audience!".as_bytes();
116
116
///
117
- /// let a: [u8; 3] = sample.choose_multiple_array (&mut rng).unwrap();
117
+ /// let a: [u8; 3] = sample.sample_array (&mut rng).unwrap();
118
118
/// ```
119
- fn choose_multiple_array < R , const N : usize > ( & self , rng : & mut R ) -> Option < [ Self :: Output ; N ] >
119
+ fn sample_array < R , const N : usize > ( & self , rng : & mut R ) -> Option < [ Self :: Output ; N ] >
120
120
where
121
121
Self :: Output : Clone + Sized ,
122
122
R : Rng + ?Sized ,
@@ -173,7 +173,7 @@ pub trait IndexedRandom: Index<usize> {
173
173
174
174
/// Biased sampling of `amount` distinct elements
175
175
///
176
- /// Similar to [`choose_multiple `], but where the likelihood of each
176
+ /// Similar to [`sample `], but where the likelihood of each
177
177
/// element's inclusion in the output may be specified. Zero-weighted
178
178
/// elements are never returned; the result may therefore contain fewer
179
179
/// elements than `amount` even when `self.len() >= amount`. The elements
@@ -198,13 +198,13 @@ pub trait IndexedRandom: Index<usize> {
198
198
/// // (50% * 50%) + (25% * 67%) = 41.7% chance that the output is `['a', 'b']` in some order.
199
199
/// // (50% * 50%) + (25% * 67%) = 41.7% chance that the output is `['a', 'c']` in some order.
200
200
/// // (25% * 33%) + (25% * 33%) = 16.6% chance that the output is `['b', 'c']` in some order.
201
- /// println!("{:?}", choices.choose_multiple_weighted (&mut rng, 2, |item| item.1).unwrap().collect::<Vec<_>>());
201
+ /// println!("{:?}", choices.sample_weighted (&mut rng, 2, |item| item.1).unwrap().collect::<Vec<_>>());
202
202
/// ```
203
- /// [`choose_multiple `]: IndexedRandom::choose_multiple
203
+ /// [`sample `]: IndexedRandom::sample
204
204
// Note: this is feature-gated on std due to usage of f64::powf.
205
205
// If necessary, we may use alloc+libm as an alternative (see PR #1089).
206
206
#[ cfg( feature = "std" ) ]
207
- fn choose_multiple_weighted < R , F , X > (
207
+ fn sample_weighted < R , F , X > (
208
208
& self ,
209
209
rng : & mut R ,
210
210
amount : usize ,
@@ -412,7 +412,7 @@ impl<T> SliceRandom for [T] {
412
412
/// An iterator over multiple slice elements.
413
413
///
414
414
/// This struct is created by
415
- /// [`IndexedRandom::choose_multiple `](trait.IndexedRandom.html#tymethod.choose_multiple ).
415
+ /// [`IndexedRandom::sample `](trait.IndexedRandom.html#tymethod.sample ).
416
416
#[ cfg( feature = "alloc" ) ]
417
417
#[ derive( Debug ) ]
418
418
pub struct SliceChooseIter < ' a , S : ?Sized + ' a , T : ' a > {
@@ -492,16 +492,13 @@ mod test {
492
492
assert_eq ! ( nums. choose_mut( & mut r) , Some ( & mut 3 ) ) ;
493
493
494
494
assert_eq ! (
495
- & chars. choose_multiple_array ( & mut r) ,
495
+ & chars. sample_array ( & mut r) ,
496
496
& Some ( [ 'f' , 'i' , 'd' , 'b' , 'c' , 'm' , 'j' , 'k' ] )
497
497
) ;
498
498
499
499
#[ cfg( feature = "alloc" ) ]
500
500
assert_eq ! (
501
- & chars
502
- . choose_multiple( & mut r, 8 )
503
- . cloned( )
504
- . collect:: <Vec <char >>( ) ,
501
+ & chars. sample( & mut r, 8 ) . cloned( ) . collect:: <Vec <char >>( ) ,
505
502
& [ 'h' , 'm' , 'd' , 'b' , 'c' , 'e' , 'n' , 'f' ]
506
503
) ;
507
504
@@ -668,7 +665,7 @@ mod test {
668
665
let choices = [ ( 'a' , 2 ) , ( 'b' , 1 ) , ( 'c' , 0 ) ] ;
669
666
for _ in 0 ..100 {
670
667
let result = choices
671
- . choose_multiple_weighted ( & mut rng, 2 , |item| item. 1 )
668
+ . sample_weighted ( & mut rng, 2 , |item| item. 1 )
672
669
. unwrap ( )
673
670
. collect :: < Vec < _ > > ( ) ;
674
671
@@ -678,29 +675,29 @@ mod test {
678
675
679
676
// Case 2: All of the weights are 0
680
677
let choices = [ ( 'a' , 0 ) , ( 'b' , 0 ) , ( 'c' , 0 ) ] ;
681
- let r = choices. choose_multiple_weighted ( & mut rng, 2 , |item| item. 1 ) ;
678
+ let r = choices. sample_weighted ( & mut rng, 2 , |item| item. 1 ) ;
682
679
assert_eq ! ( r. unwrap( ) . len( ) , 0 ) ;
683
680
684
681
// Case 3: Negative weights
685
682
let choices = [ ( 'a' , -1 ) , ( 'b' , 1 ) , ( 'c' , 1 ) ] ;
686
- let r = choices. choose_multiple_weighted ( & mut rng, 2 , |item| item. 1 ) ;
683
+ let r = choices. sample_weighted ( & mut rng, 2 , |item| item. 1 ) ;
687
684
assert_eq ! ( r. unwrap_err( ) , WeightError :: InvalidWeight ) ;
688
685
689
686
// Case 4: Empty list
690
687
let choices = [ ] ;
691
- let r = choices. choose_multiple_weighted ( & mut rng, 0 , |_: & ( ) | 0 ) ;
688
+ let r = choices. sample_weighted ( & mut rng, 0 , |_: & ( ) | 0 ) ;
692
689
assert_eq ! ( r. unwrap( ) . count( ) , 0 ) ;
693
690
694
691
// Case 5: NaN weights
695
692
let choices = [ ( 'a' , f64:: NAN ) , ( 'b' , 1.0 ) , ( 'c' , 1.0 ) ] ;
696
- let r = choices. choose_multiple_weighted ( & mut rng, 2 , |item| item. 1 ) ;
693
+ let r = choices. sample_weighted ( & mut rng, 2 , |item| item. 1 ) ;
697
694
assert_eq ! ( r. unwrap_err( ) , WeightError :: InvalidWeight ) ;
698
695
699
696
// Case 6: +infinity weights
700
697
let choices = [ ( 'a' , f64:: INFINITY ) , ( 'b' , 1.0 ) , ( 'c' , 1.0 ) ] ;
701
698
for _ in 0 ..100 {
702
699
let result = choices
703
- . choose_multiple_weighted ( & mut rng, 2 , |item| item. 1 )
700
+ . sample_weighted ( & mut rng, 2 , |item| item. 1 )
704
701
. unwrap ( )
705
702
. collect :: < Vec < _ > > ( ) ;
706
703
assert_eq ! ( result. len( ) , 2 ) ;
@@ -709,12 +706,12 @@ mod test {
709
706
710
707
// Case 7: -infinity weights
711
708
let choices = [ ( 'a' , f64:: NEG_INFINITY ) , ( 'b' , 1.0 ) , ( 'c' , 1.0 ) ] ;
712
- let r = choices. choose_multiple_weighted ( & mut rng, 2 , |item| item. 1 ) ;
709
+ let r = choices. sample_weighted ( & mut rng, 2 , |item| item. 1 ) ;
713
710
assert_eq ! ( r. unwrap_err( ) , WeightError :: InvalidWeight ) ;
714
711
715
712
// Case 8: -0 weights
716
713
let choices = [ ( 'a' , -0.0 ) , ( 'b' , 1.0 ) , ( 'c' , 1.0 ) ] ;
717
- let r = choices. choose_multiple_weighted ( & mut rng, 2 , |item| item. 1 ) ;
714
+ let r = choices. sample_weighted ( & mut rng, 2 , |item| item. 1 ) ;
718
715
assert ! ( r. is_ok( ) ) ;
719
716
}
720
717
@@ -737,7 +734,7 @@ mod test {
737
734
let expected_results = [ 5833 , 2667 , 1500 ] ;
738
735
for _ in 0 ..10000 {
739
736
let result = choices
740
- . choose_multiple_weighted ( & mut rng, 2 , |item| item. 1 )
737
+ . sample_weighted ( & mut rng, 2 , |item| item. 1 )
741
738
. unwrap ( )
742
739
. collect :: < Vec < _ > > ( ) ;
743
740
0 commit comments