Skip to content

Commit 235b7d1

Browse files
authored
Merge pull request #347 from pitdicker/gen_bool_accuracy_note
Add accuracy note to gen_bool
2 parents f6d1259 + 6df9d1f commit 235b7d1

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,16 @@ pub trait Rng: RngCore {
515515
/// let mut rng = thread_rng();
516516
/// println!("{}", rng.gen_bool(1.0 / 3.0));
517517
/// ```
518+
///
519+
/// # Accuracy note
520+
///
521+
/// `gen_bool` uses 32 bits of the RNG, so if you use it to generate close
522+
/// to or more than 2^32 results, a tiny bias may become noticable.
523+
/// A notable consequence of the method used here is that the worst case is
524+
/// `rng.gen_bool(0.0)`: it has a chance of 1 in 2^32 of being true, while
525+
/// it should always be false. But using `gen_bool` to consume *many* values
526+
/// from an RNG just to consistently generate `false` does not match with
527+
/// the intent of this method.
518528
fn gen_bool(&mut self, p: f64) -> bool {
519529
assert!(p >= 0.0 && p <= 1.0);
520530
// If `p` is constant, this will be evaluated at compile-time.

0 commit comments

Comments
 (0)