184
184
//! // where the car is. The game host will never open the door with the car.
185
185
//! fn game_host_open<R: Rng>(car: u32, choice: u32, rng: &mut R) -> u32 {
186
186
//! let choices = free_doors(&[car, choice]);
187
- //! rand::sample (rng, choices.into_iter(), 1)[0]
187
+ //! rand::sample_reservoir (rng, choices.into_iter(), 1)[0]
188
188
//! }
189
189
//!
190
190
//! // Returns the door we switch to, given our current choice and
@@ -260,6 +260,12 @@ pub use os::OsRng;
260
260
261
261
pub use isaac:: { IsaacRng , Isaac64Rng } ;
262
262
pub use chacha:: ChaChaRng ;
263
+ pub use sample:: {
264
+ // TODO: `sample` name will be deprecated in 1.0, use `sample_reservoir` instead
265
+ sample_reservoir as sample,
266
+ sample_reservoir,
267
+ Sample ,
268
+ SampleRef } ;
263
269
264
270
#[ cfg( target_pointer_width = "32" ) ]
265
271
use IsaacRng as IsaacWordRng ;
@@ -276,6 +282,7 @@ pub mod reseeding;
276
282
mod rand_impls;
277
283
pub mod os;
278
284
pub mod read;
285
+ mod sample;
279
286
280
287
#[ allow( bad_style) ]
281
288
type w64 = w < u64 > ;
@@ -1016,40 +1023,9 @@ pub fn random<T: Rand>() -> T {
1016
1023
thread_rng ( ) . gen ( )
1017
1024
}
1018
1025
1019
- /// Randomly sample up to `amount` elements from a finite iterator.
1020
- /// The order of elements in the sample is not random.
1021
- ///
1022
- /// # Example
1023
- ///
1024
- /// ```rust
1025
- /// use rand::{thread_rng, sample};
1026
- ///
1027
- /// let mut rng = thread_rng();
1028
- /// let sample = sample(&mut rng, 1..100, 5);
1029
- /// println!("{:?}", sample);
1030
- /// ```
1031
- pub fn sample < T , I , R > ( rng : & mut R , iterable : I , amount : usize ) -> Vec < T >
1032
- where I : IntoIterator < Item =T > ,
1033
- R : Rng ,
1034
- {
1035
- let mut iter = iterable. into_iter ( ) ;
1036
- let mut reservoir: Vec < T > = iter. by_ref ( ) . take ( amount) . collect ( ) ;
1037
- // continue unless the iterator was exhausted
1038
- if reservoir. len ( ) == amount {
1039
- for ( i, elem) in iter. enumerate ( ) {
1040
- let k = rng. gen_range ( 0 , i + 1 + amount) ;
1041
- if let Some ( spot) = reservoir. get_mut ( k) {
1042
- * spot = elem;
1043
- }
1044
- }
1045
- }
1046
- reservoir
1047
- }
1048
-
1049
1026
#[ cfg( test) ]
1050
1027
mod test {
1051
- use super :: { Rng , thread_rng, random, SeedableRng , StdRng , sample,
1052
- weak_rng} ;
1028
+ use super :: { Rng , thread_rng, random, SeedableRng , StdRng , weak_rng} ;
1053
1029
use std:: iter:: repeat;
1054
1030
1055
1031
pub struct MyRng < R > { inner : R }
@@ -1255,24 +1231,6 @@ mod test {
1255
1231
( f32 , ( f64 , ( f64 , ) ) ) ) = random ( ) ;
1256
1232
}
1257
1233
1258
- #[ test]
1259
- fn test_sample ( ) {
1260
- let min_val = 1 ;
1261
- let max_val = 100 ;
1262
-
1263
- let mut r = thread_rng ( ) ;
1264
- let vals = ( min_val..max_val) . collect :: < Vec < i32 > > ( ) ;
1265
- let small_sample = sample ( & mut r, vals. iter ( ) , 5 ) ;
1266
- let large_sample = sample ( & mut r, vals. iter ( ) , vals. len ( ) + 5 ) ;
1267
-
1268
- assert_eq ! ( small_sample. len( ) , 5 ) ;
1269
- assert_eq ! ( large_sample. len( ) , vals. len( ) ) ;
1270
-
1271
- assert ! ( small_sample. iter( ) . all( |e| {
1272
- * * e >= min_val && * * e <= max_val
1273
- } ) ) ;
1274
- }
1275
-
1276
1234
#[ test]
1277
1235
fn test_std_rng_seeded ( ) {
1278
1236
let s = thread_rng ( ) . gen_iter :: < usize > ( ) . take ( 256 ) . collect :: < Vec < usize > > ( ) ;
0 commit comments