Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rand"
version = "0.3.18"
version = "0.3.19"
authors = ["The Rust Project Developers"]
license = "MIT/Apache-2.0"
readme = "README.md"
Expand Down
43 changes: 10 additions & 33 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 @@ -276,6 +276,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,6 +1017,10 @@ pub fn random<T: Rand>() -> T {
thread_rng().gen()
}

#[inline(always)]
#[deprecated(since="0.3.18", note="renamed to seq::sample_iter")]
/// DEPRECATED: use `seq::sample_iter` instead.
///
/// Randomly sample up to `amount` elements from a finite iterator.
/// The order of elements in the sample is not random.
///
Expand All @@ -1032,24 +1037,14 @@ 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
// the legacy sample didn't care whether amount was met
seq::sample_iter(rng, iterable, amount)
.unwrap_or_else(|e| e)
}

#[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 +1250,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