Skip to content

Commit 4acef1b

Browse files
authored
Merge pull request #308 from pitdicker/deprecate_weighted_bool
Deprecate Rng::gen_weighted_bool
2 parents 4634912 + a20c7b1 commit 4acef1b

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

benches/generators.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const BYTES_LEN: usize = 1024;
99
use std::mem::size_of;
1010
use test::{black_box, Bencher};
1111

12-
use rand::{RngCore, Rng, SeedableRng, NewRng, StdRng, OsRng, JitterRng, EntropyRng};
12+
use rand::{RngCore, Rng, SeedableRng, NewRng};
13+
use rand::{StdRng, SmallRng, OsRng, JitterRng, EntropyRng};
1314
use rand::{XorShiftRng, Hc128Rng, IsaacRng, Isaac64Rng, ChaChaRng};
1415
use rand::reseeding::ReseedingRng;
1516
use rand::prng::hc128::Hc128Core;
@@ -37,6 +38,7 @@ gen_bytes!(gen_bytes_hc128, Hc128Rng::new());
3738
gen_bytes!(gen_bytes_isaac, IsaacRng::new());
3839
gen_bytes!(gen_bytes_isaac64, Isaac64Rng::new());
3940
gen_bytes!(gen_bytes_std, StdRng::new());
41+
gen_bytes!(gen_bytes_small, SmallRng::new());
4042
gen_bytes!(gen_bytes_os, OsRng::new().unwrap());
4143

4244
macro_rules! gen_uint {
@@ -61,13 +63,15 @@ gen_uint!(gen_u32_hc128, u32, Hc128Rng::new());
6163
gen_uint!(gen_u32_isaac, u32, IsaacRng::new());
6264
gen_uint!(gen_u32_isaac64, u32, Isaac64Rng::new());
6365
gen_uint!(gen_u32_std, u32, StdRng::new());
66+
gen_uint!(gen_u32_small, u32, SmallRng::new());
6467
gen_uint!(gen_u32_os, u32, OsRng::new().unwrap());
6568

6669
gen_uint!(gen_u64_xorshift, u64, XorShiftRng::new());
6770
gen_uint!(gen_u64_hc128, u64, Hc128Rng::new());
6871
gen_uint!(gen_u64_isaac, u64, IsaacRng::new());
6972
gen_uint!(gen_u64_isaac64, u64, Isaac64Rng::new());
7073
gen_uint!(gen_u64_std, u64, StdRng::new());
74+
gen_uint!(gen_u64_small, u64, SmallRng::new());
7175
gen_uint!(gen_u64_os, u64, OsRng::new().unwrap());
7276

7377
// Do not test JitterRng like the others by running it RAND_BENCH_N times per,

benches/misc.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,39 @@
33
extern crate test;
44
extern crate rand;
55

6+
const RAND_BENCH_N: u64 = 1000;
7+
68
use test::{black_box, Bencher};
79

810
use rand::{SeedableRng, SmallRng, Rng, thread_rng};
911
use rand::seq::*;
1012

13+
#[bench]
14+
fn misc_gen_bool(b: &mut Bencher) {
15+
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
16+
b.iter(|| {
17+
let mut accum = true;
18+
for _ in 0..::RAND_BENCH_N {
19+
accum ^= rng.gen_bool(0.18);
20+
}
21+
black_box(accum);
22+
})
23+
}
24+
25+
#[bench]
26+
fn misc_gen_bool_var(b: &mut Bencher) {
27+
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
28+
b.iter(|| {
29+
let mut p = 0.18;
30+
let mut accum = true;
31+
for _ in 0..::RAND_BENCH_N {
32+
accum ^= rng.gen_bool(p);
33+
p += 0.0001;
34+
}
35+
black_box(accum);
36+
})
37+
}
38+
1139
#[bench]
1240
fn misc_shuffle_100(b: &mut Bencher) {
1341
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();

src/distributions/binomial.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl Distribution<u64> for Binomial {
6464
if expected < 25.0 {
6565
let mut lresult = 0.0;
6666
for _ in 0 .. self.n {
67-
if rng.gen::<f64>() < p {
67+
if rng.gen_bool(p) {
6868
lresult += 1.0;
6969
}
7070
}

src/lib.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ pub trait Rng: RngCore {
521521
/// # Example
522522
///
523523
/// ```rust
524+
/// #[allow(deprecated)]
524525
/// use rand::{thread_rng, Rng};
525526
///
526527
/// let mut rng = thread_rng();
@@ -532,11 +533,29 @@ pub trait Rng: RngCore {
532533
/// // First meaningful use of `gen_weighted_bool`.
533534
/// println!("{}", rng.gen_weighted_bool(3));
534535
/// ```
536+
#[deprecated(since="0.5.0", note="use gen_bool instead")]
535537
fn gen_weighted_bool(&mut self, n: u32) -> bool {
536538
// Short-circuit after `n <= 1` to avoid panic in `gen_range`
537539
n <= 1 || self.gen_range(0, n) == 0
538540
}
539541

542+
/// Return a bool with a probability `p` of being true.
543+
///
544+
/// # Example
545+
///
546+
/// ```rust
547+
/// use rand::{thread_rng, Rng};
548+
///
549+
/// let mut rng = thread_rng();
550+
/// println!("{}", rng.gen_bool(1.0 / 3.0));
551+
/// ```
552+
fn gen_bool(&mut self, p: f64) -> bool {
553+
assert!(p >= 0.0 && p <= 1.0);
554+
// If `p` is constant, this will be evaluated at compile-time.
555+
let p_int = (p * core::u32::MAX as f64) as u32;
556+
self.gen::<u32>() <= p_int
557+
}
558+
540559
/// Return an iterator of random characters from the set A-Z,a-z,0-9.
541560
///
542561
/// # Example
@@ -798,7 +817,7 @@ impl<R: SeedableRng> NewRng for R {
798817
}
799818
}
800819

801-
/// The standard RNG. The PRNG algorithm in `StdRng` is choosen to be efficient
820+
/// The standard RNG. The PRNG algorithm in `StdRng` is chosen to be efficient
802821
/// on the current platform, to be statistically strong and unpredictable
803822
/// (meaning a cryptographically secure PRNG).
804823
///
@@ -847,7 +866,7 @@ impl SeedableRng for StdRng {
847866
}
848867

849868
/// An RNG recommended when small state, cheap initialization and good
850-
/// performance are required. The PRNG algorithm in `SmallRng` is choosen to be
869+
/// performance are required. The PRNG algorithm in `SmallRng` is chosen to be
851870
/// efficient on the current platform, **without consideration for cryptography
852871
/// or security**. The size of its state is much smaller than for `StdRng`.
853872
///
@@ -890,10 +909,12 @@ impl SeedableRng for StdRng {
890909
pub struct SmallRng(XorShiftRng);
891910

892911
impl RngCore for SmallRng {
912+
#[inline(always)]
893913
fn next_u32(&mut self) -> u32 {
894914
self.0.next_u32()
895915
}
896916

917+
#[inline(always)]
897918
fn next_u64(&mut self) -> u64 {
898919
self.0.next_u64()
899920
}
@@ -1076,12 +1097,22 @@ mod test {
10761097
}
10771098

10781099
#[test]
1100+
#[allow(deprecated)]
10791101
fn test_gen_weighted_bool() {
10801102
let mut r = rng(104);
10811103
assert_eq!(r.gen_weighted_bool(0), true);
10821104
assert_eq!(r.gen_weighted_bool(1), true);
10831105
}
10841106

1107+
#[test]
1108+
fn test_gen_bool() {
1109+
let mut r = rng(105);
1110+
for _ in 0..5 {
1111+
assert_eq!(r.gen_bool(0.0), false);
1112+
assert_eq!(r.gen_bool(1.0), true);
1113+
}
1114+
}
1115+
10851116
#[test]
10861117
fn test_choose() {
10871118
let mut r = rng(107);

0 commit comments

Comments
 (0)