Skip to content

Commit 27390c4

Browse files
committed
Seq sample code: increase error bounds in test, simplify sample code
1 parent 3d3efc7 commit 27390c4

File tree

2 files changed

+4
-13
lines changed

2 files changed

+4
-13
lines changed

benches/seq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ fn seq_iter_unhinted_choose_from_100(b: &mut Bencher) {
127127
#[bench]
128128
fn seq_iter_window_hinted_choose_from_100(b: &mut Bencher) {
129129
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
130-
let x : &[usize] = &[1; 100];
130+
let x : &[usize] = &[1; 1000];
131131
b.iter(|| {
132132
WindowHintedIterator { iter: x.iter(), window_size: 7 }.choose(&mut rng)
133133
})

src/seq/mod.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,7 @@ pub trait IteratorRandom: Iterator + Sized {
191191
let mut result = None;
192192

193193
if upper == Some(lower) {
194-
// Remove this once we can specialize on ExactSizeIterator
195194
return if lower == 0 { None } else { self.nth(rng.gen_range(0, lower)) };
196-
} else if lower <= 1 {
197-
result = self.next();
198-
if result.is_none() {
199-
return result;
200-
}
201-
consumed = 1;
202-
let hint = self.size_hint();
203-
lower = hint.0;
204-
upper = hint.1;
205195
}
206196

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

0 commit comments

Comments
 (0)