Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 5 additions & 51 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
//! // where the car is. The game host will never open the door with the car.
//! fn game_host_open<R: Rng>(car: u32, choice: u32, rng: &mut R) -> u32 {
//! let choices = free_doors(&[car, choice]);
//! rand::sample(rng, choices.into_iter(), 1)[0]
//! rand::seq::sample_slice(rng, &choices, 1)[0]
//! }
//!
//! // Returns the door we switch to, given our current choice and
Expand Down Expand Up @@ -260,6 +260,8 @@ pub use os::OsRng;

pub use isaac::{IsaacRng, Isaac64Rng};
pub use chacha::ChaChaRng;
#[deprecated(since="0.3.18", note="renamed to seq::sample_iter")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the current version; I guess this should be 0.3.19 with a corresponding version bump in Cargo.toml.

pub use seq::{sample_iter as sample};

#[cfg(target_pointer_width = "32")]
use IsaacRng as IsaacWordRng;
Expand All @@ -276,6 +278,7 @@ pub mod reseeding;
mod rand_impls;
pub mod os;
pub mod read;
pub mod seq;

#[allow(bad_style)]
type w64 = w<u64>;
Expand Down Expand Up @@ -1016,40 +1019,9 @@ pub fn random<T: Rand>() -> T {
thread_rng().gen()
}

/// Randomly sample up to `amount` elements from a finite iterator.
/// The order of elements in the sample is not random.
///
/// # Example
///
/// ```rust
/// use rand::{thread_rng, sample};
///
/// let mut rng = thread_rng();
/// let sample = sample(&mut rng, 1..100, 5);
/// println!("{:?}", sample);
/// ```
pub fn sample<T, I, R>(rng: &mut R, iterable: I, amount: usize) -> Vec<T>
where I: IntoIterator<Item=T>,
R: Rng,
{
let mut iter = iterable.into_iter();
let mut reservoir: Vec<T> = iter.by_ref().take(amount).collect();
// continue unless the iterator was exhausted
if reservoir.len() == amount {
for (i, elem) in iter.enumerate() {
let k = rng.gen_range(0, i + 1 + amount);
if let Some(spot) = reservoir.get_mut(k) {
*spot = elem;
}
}
}
reservoir
}

#[cfg(test)]
mod test {
use super::{Rng, thread_rng, random, SeedableRng, StdRng, sample,
weak_rng};
use super::{Rng, thread_rng, random, SeedableRng, StdRng, weak_rng};
use std::iter::repeat;

pub struct MyRng<R> { inner: R }
Expand Down Expand Up @@ -1255,24 +1227,6 @@ mod test {
(f32, (f64, (f64,)))) = random();
}

#[test]
fn test_sample() {
let min_val = 1;
let max_val = 100;

let mut r = thread_rng();
let vals = (min_val..max_val).collect::<Vec<i32>>();
let small_sample = sample(&mut r, vals.iter(), 5);
let large_sample = sample(&mut r, vals.iter(), vals.len() + 5);

assert_eq!(small_sample.len(), 5);
assert_eq!(large_sample.len(), vals.len());

assert!(small_sample.iter().all(|e| {
**e >= min_val && **e <= max_val
}));
}

#[test]
fn test_std_rng_seeded() {
let s = thread_rng().gen_iter::<usize>().take(256).collect::<Vec<usize>>();
Expand Down
Loading