Skip to content

Commit b0a5e4f

Browse files
committed
Deprecate Rng::gen_weighted_bool
1 parent b46f8b1 commit b0a5e4f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,9 +518,21 @@ pub trait Rng: RngCore {
518518

519519
/// Return a bool with a 1 in n chance of true
520520
///
521+
/// # Deprecated
522+
/// Examples of what to replace `rng.gen_weighted_bool(3)` with
523+
/// (from fastest to slowest):
524+
/// ```
525+
/// # use rand::{thread_rng, Rng};
526+
/// # let mut rng = thread_rng();
527+
/// rng.gen::<u32>() < std::u32::MAX / 3;
528+
/// rng.gen::<f32>() < (1.0f32 / 3.0);
529+
/// rng.gen_range(0, 3) < 1;
530+
/// ```
531+
///
521532
/// # Example
522533
///
523534
/// ```rust
535+
/// #[allow(deprecated)]
524536
/// use rand::{thread_rng, Rng};
525537
///
526538
/// let mut rng = thread_rng();
@@ -532,6 +544,7 @@ pub trait Rng: RngCore {
532544
/// // First meaningful use of `gen_weighted_bool`.
533545
/// println!("{}", rng.gen_weighted_bool(3));
534546
/// ```
547+
#[deprecated(since="0.5.0", note="use gen_range(0, n) < 1 instead")]
535548
fn gen_weighted_bool(&mut self, n: u32) -> bool {
536549
// Short-circuit after `n <= 1` to avoid panic in `gen_range`
537550
n <= 1 || self.gen_range(0, n) == 0
@@ -1076,6 +1089,7 @@ mod test {
10761089
}
10771090

10781091
#[test]
1092+
#[allow(deprecated)]
10791093
fn test_gen_weighted_bool() {
10801094
let mut r = rng(104);
10811095
assert_eq!(r.gen_weighted_bool(0), true);

0 commit comments

Comments
 (0)