Skip to content

Commit

Permalink
Seq sample code: increase error bounds in test, simplify sample code
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Sep 20, 2018
1 parent 3d3efc7 commit 27390c4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
2 changes: 1 addition & 1 deletion benches/seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn seq_iter_unhinted_choose_from_100(b: &mut Bencher) {
#[bench]
fn seq_iter_window_hinted_choose_from_100(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
let x : &[usize] = &[1; 100];
let x : &[usize] = &[1; 1000];
b.iter(|| {
WindowHintedIterator { iter: x.iter(), window_size: 7 }.choose(&mut rng)
})
Expand Down
15 changes: 3 additions & 12 deletions src/seq/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,7 @@ pub trait IteratorRandom: Iterator + Sized {
let mut result = None;

if upper == Some(lower) {
// Remove this once we can specialize on ExactSizeIterator
return if lower == 0 { None } else { self.nth(rng.gen_range(0, lower)) };
} else if lower <= 1 {
result = self.next();
if result.is_none() {
return result;
}
consumed = 1;
let hint = self.size_hint();
lower = hint.0;
upper = hint.1;
}

// Continue until the iterator is exhausted
Expand Down Expand Up @@ -617,8 +607,9 @@ mod test {
}
for count in chosen.iter() {
// Samples should follow Binomial(1000, 1/9)
let err = *count - 1000 / 9;
assert!(-30 <= err && err <= 30);
// Octave: binopdf(x, 1000, 1/9) gives the prob of *count == x
// Note: have seen 153, which is unlikely but not impossible.
assert!(72 < *count && *count < 154, "count not close to 1000/9: {}", count);
}
}

Expand Down

0 comments on commit 27390c4

Please sign in to comment.