Skip to content

Commit

Permalink
Merge pull request rust-random#282 from pitdicker/gen_weighted_bool_doc
Browse files Browse the repository at this point in the history
Clarify `gen_weighted_bool` behaviour
  • Loading branch information
dhardy committed Mar 4, 2018
2 parents 1646ddc + 2da3107 commit 8245d5f
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,9 +641,16 @@ pub trait Rng: RngCore + Sized {
/// use rand::{thread_rng, Rng};
///
/// let mut rng = thread_rng();
/// assert_eq!(rng.gen_weighted_bool(0), true);
/// assert_eq!(rng.gen_weighted_bool(1), true);
/// // Just like `rng.gen::<bool>()` a 50-50% chance, but using a slower
/// // method with different results.
/// println!("{}", rng.gen_weighted_bool(2));
/// // First meaningful use of `gen_weighted_bool`.
/// println!("{}", rng.gen_weighted_bool(3));
/// ```
fn gen_weighted_bool(&mut self, n: u32) -> bool {
// Short-circuit after `n <= 1` to avoid panic in `gen_range`
n <= 1 || self.gen_range(0, n) == 0
}

Expand Down

0 comments on commit 8245d5f

Please sign in to comment.