Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add accuracy note to gen_bool #347

Merged
merged 1 commit into from
Apr 1, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,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