Skip to content

Commit 6d5041d

Browse files
committed
accurate IteratorRandom::choose()
f64 reciprocal is inexact, with residuals on the order of 1 / 2^54. For example: the probability gen_bool(1.0 / 3) == true is: 6004799503160661/18014398509481984 Using gen_range is exact for all values of `consumed`. NOTE: this is a value stability-breaking change fixes #1058
1 parent 47506e0 commit 6d5041d

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

src/seq/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,7 @@ pub trait IteratorRandom: Iterator + Sized {
324324
return result;
325325
}
326326
consumed += 1;
327-
let denom = consumed as f64; // accurate to 2^53 elements
328-
if rng.gen_bool(1.0 / denom) {
327+
if gen_index(rng, consumed) == 0 {
329328
result = elem;
330329
}
331330
}

0 commit comments

Comments
 (0)