We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 1646ddc + 2da3107 commit 8245d5fCopy full SHA for 8245d5f
src/lib.rs
@@ -641,9 +641,16 @@ pub trait Rng: RngCore + Sized {
641
/// use rand::{thread_rng, Rng};
642
///
643
/// let mut rng = thread_rng();
644
+ /// assert_eq!(rng.gen_weighted_bool(0), true);
645
+ /// assert_eq!(rng.gen_weighted_bool(1), true);
646
+ /// // Just like `rng.gen::<bool>()` a 50-50% chance, but using a slower
647
+ /// // method with different results.
648
+ /// println!("{}", rng.gen_weighted_bool(2));
649
+ /// // First meaningful use of `gen_weighted_bool`.
650
/// println!("{}", rng.gen_weighted_bool(3));
651
/// ```
652
fn gen_weighted_bool(&mut self, n: u32) -> bool {
653
+ // Short-circuit after `n <= 1` to avoid panic in `gen_range`
654
n <= 1 || self.gen_range(0, n) == 0
655
}
656
0 commit comments