File tree Expand file tree Collapse file tree 1 file changed +7
-0
lines changed Expand file tree Collapse file tree 1 file changed +7
-0
lines changed Original file line number Diff line number Diff line change @@ -641,9 +641,16 @@ pub trait Rng: RngCore + Sized {
641
641
/// use rand::{thread_rng, Rng};
642
642
///
643
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`.
644
650
/// println!("{}", rng.gen_weighted_bool(3));
645
651
/// ```
646
652
fn gen_weighted_bool ( & mut self , n : u32 ) -> bool {
653
+ // Short-circuit after `n <= 1` to avoid panic in `gen_range`
647
654
n <= 1 || self . gen_range ( 0 , n) == 0
648
655
}
649
656
You can’t perform that action at this time.
0 commit comments