Skip to content

Commit

Permalink
Merge pull request #347 from pitdicker/gen_bool_accuracy_note
Browse files Browse the repository at this point in the history
Add accuracy note to gen_bool
  • Loading branch information
dhardy authored Apr 1, 2018
2 parents 3ec525a + ca4722b commit 683d6b9
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,16 @@ pub trait Rng: RngCore {
/// let mut rng = thread_rng();
/// println!("{}", rng.gen_bool(1.0 / 3.0));
/// ```
///
/// # Accuracy note
///
/// `gen_bool` uses 32 bits of the RNG, so if you use it to generate close
/// to or more than 2^32 results, a tiny bias may become noticable.
/// A notable consequence of the method used here is that the worst case is
/// `rng.gen_bool(0.0)`: it has a chance of 1 in 2^32 of being true, while
/// it should always be false. But using `gen_bool` to consume *many* values
/// from an RNG just to consistently generate `false` does not match with
/// the intent of this method.
fn gen_bool(&mut self, p: f64) -> bool {
assert!(p >= 0.0 && p <= 1.0);
// If `p` is constant, this will be evaluated at compile-time.
Expand Down

0 comments on commit 683d6b9

Please sign in to comment.