Skip to content

Commit

Permalink
Deprecate Rng::gen_weighted_bool
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Mar 17, 2018
1 parent b46f8b1 commit 3b06ad0
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,17 @@ pub trait Rng: RngCore {

/// Return a bool with a 1 in n chance of true
///
/// # Deprecated
/// Examples of what to replace `rng.gen_weighted_bool(3)` with
/// (from fastest to slowest):
/// ```
/// # use rand::{thread_rng, Rng};
/// # let mut rng = thread_rng();
/// rng.gen::<u32>() < std::u32::MAX / 3;
/// rng.gen::<f32>() < (1.0f32 / 3.0);
/// rng.gen_range(0, 3) < 1;
/// ```
///
/// # Example
///
/// ```rust
Expand All @@ -532,6 +543,7 @@ pub trait Rng: RngCore {
/// // First meaningful use of `gen_weighted_bool`.
/// println!("{}", rng.gen_weighted_bool(3));
/// ```
#[deprecated(since="0.5.0", note="use gen_range(0, n) < 1 instead")]
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 Expand Up @@ -1076,6 +1088,7 @@ mod test {
}

#[test]
#[allow(deprecated)]
fn test_gen_weighted_bool() {
let mut r = rng(104);
assert_eq!(r.gen_weighted_bool(0), true);
Expand Down

0 comments on commit 3b06ad0

Please sign in to comment.