Skip to content

Commit cd3354b

Browse files
committed
gen_ratio -> random_probability, gen_bool -> random_probability_f64
1 parent 7fe350c commit cd3354b

File tree

7 files changed

+39
-39
lines changed

7 files changed

+39
-39
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
2929
- Rename `Rng::gen_iter` to `random_iter` (#1500)
3030
- Rename `rand::thread_rng()` to `rand::rng()`, and remove from the prelude (#1506)
3131
- Remove `rand::random()` from the prelude (#1506)
32-
- Rename `Rng::gen_range` to `random_range`, `gen_bool` to `random_bool`, `gen_ratio` to `random_ratio` (#1505)
32+
- Rename `Rng::gen_range` to `random_range`, `gen_ratio` to `random_probability`, `gen_bool` to `random_probability_f64` (#1505, #1527)
3333

3434
## [0.9.0-alpha.1] - 2024-03-18
3535
- Add the `Slice::num_choices` method to the Slice distribution (#1402)

benches/benches/bool.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ criterion_group!(
2121
criterion_main!(benches);
2222

2323
pub fn bench(c: &mut Criterion) {
24-
let mut g = c.benchmark_group("random_bool");
24+
let mut g = c.benchmark_group("random_probability_f64");
2525
g.sample_size(1000);
2626
g.warm_up_time(core::time::Duration::from_millis(500));
2727
g.measurement_time(core::time::Duration::from_millis(1000));
@@ -33,25 +33,25 @@ pub fn bench(c: &mut Criterion) {
3333

3434
g.bench_function("const", |b| {
3535
let mut rng = Pcg32::from_rng(&mut rand::rng());
36-
b.iter(|| rng.random_bool(0.18))
36+
b.iter(|| rng.random_probability_f64(0.18))
3737
});
3838

3939
g.bench_function("var", |b| {
4040
let mut rng = Pcg32::from_rng(&mut rand::rng());
4141
let p = rng.random();
42-
b.iter(|| rng.random_bool(p))
42+
b.iter(|| rng.random_probability_f64(p))
4343
});
4444

4545
g.bench_function("ratio_const", |b| {
4646
let mut rng = Pcg32::from_rng(&mut rand::rng());
47-
b.iter(|| rng.random_ratio(2, 3))
47+
b.iter(|| rng.random_probability(2, 3))
4848
});
4949

5050
g.bench_function("ratio_var", |b| {
5151
let mut rng = Pcg32::from_rng(&mut rand::rng());
5252
let d = rng.random_range(1..=100);
5353
let n = rng.random_range(0..=d);
54-
b.iter(|| rng.random_ratio(n, d));
54+
b.iter(|| rng.random_probability(n, d));
5555
});
5656

5757
g.bench_function("bernoulli_const", |b| {

src/distr/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
//! # Non-uniform sampling
7777
//!
7878
//! Sampling a simple true/false outcome with a given probability has a name:
79-
//! the [`Bernoulli`] distribution (this is used by [`Rng::random_bool`]).
79+
//! the [`Bernoulli`] distribution (this is used by [`Rng::random_probability_f64`]).
8080
//!
8181
//! For weighted sampling from a sequence of discrete values, use the
8282
//! [`WeightedIndex`] distribution.

src/lib.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,12 @@ where
217217
/// Return a bool with a probability `p` of being true.
218218
///
219219
/// This function is shorthand for
220-
/// <code>[rng()].[random_bool](Rng::random_bool)(<var>p</var>)</code>.
220+
/// <code>[rng()].[random_probability_f64](Rng::random_probability_f64)(<var>p</var>)</code>.
221221
///
222222
/// # Example
223223
///
224224
/// ```
225-
/// println!("{}", rand::random_bool(1.0 / 3.0));
225+
/// println!("{}", rand::random_probability_f64(1.0 / 3.0));
226226
/// ```
227227
///
228228
/// # Panics
@@ -231,14 +231,14 @@ where
231231
#[cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))]
232232
#[inline]
233233
#[track_caller]
234-
pub fn random_bool(p: f64) -> bool {
235-
rng().random_bool(p)
234+
pub fn random_probability_f64(p: f64) -> bool {
235+
rng().random_probability_f64(p)
236236
}
237237

238238
/// Return a bool with a probability of `numerator/denominator` of being
239239
/// true.
240240
///
241-
/// That is, `random_ratio(2, 3)` has chance of 2 in 3, or about 67%, of
241+
/// That is, `random_probability(2, 3)` has chance of 2 in 3, or about 67%, of
242242
/// returning true. If `numerator == denominator`, then the returned value
243243
/// is guaranteed to be `true`. If `numerator == 0`, then the returned
244244
/// value is guaranteed to be `false`.
@@ -247,7 +247,7 @@ pub fn random_bool(p: f64) -> bool {
247247
/// sampling from the same `numerator` and `denominator` repeatedly.
248248
///
249249
/// This function is shorthand for
250-
/// <code>[rng()].[random_ratio](Rng::random_ratio)(<var>numerator</var>, <var>denominator</var>)</code>.
250+
/// <code>[rng()].[random_probability](Rng::random_probability)(<var>numerator</var>, <var>denominator</var>)</code>.
251251
///
252252
/// # Panics
253253
///
@@ -256,15 +256,15 @@ pub fn random_bool(p: f64) -> bool {
256256
/// # Example
257257
///
258258
/// ```
259-
/// println!("{}", rand::random_ratio(2, 3));
259+
/// println!("{}", rand::random_probability(2, 3));
260260
/// ```
261261
///
262262
/// [`Bernoulli`]: distr::Bernoulli
263263
#[cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))]
264264
#[inline]
265265
#[track_caller]
266-
pub fn random_ratio(numerator: u32, denominator: u32) -> bool {
267-
rng().random_ratio(numerator, denominator)
266+
pub fn random_probability(numerator: u32, denominator: u32) -> bool {
267+
rng().random_probability(numerator, denominator)
268268
}
269269

270270
/// Fill any type implementing [`Fill`] with random data

src/rng.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ pub trait Rng: RngCore {
178178
/// use rand::Rng;
179179
///
180180
/// let mut rng = rand::rng();
181-
/// println!("{}", rng.random_bool(1.0 / 3.0));
181+
/// println!("{}", rng.random_probability_f64(1.0 / 3.0));
182182
/// ```
183183
///
184184
/// # Panics
@@ -188,7 +188,7 @@ pub trait Rng: RngCore {
188188
/// [`Bernoulli`]: distr::Bernoulli
189189
#[inline]
190190
#[track_caller]
191-
fn random_bool(&mut self, p: f64) -> bool {
191+
fn random_probability_f64(&mut self, p: f64) -> bool {
192192
match distr::Bernoulli::new(p) {
193193
Ok(d) => self.sample(d),
194194
Err(_) => panic!("p={:?} is outside range [0.0, 1.0]", p),
@@ -198,7 +198,7 @@ pub trait Rng: RngCore {
198198
/// Return a bool with a probability of `numerator/denominator` of being
199199
/// true.
200200
///
201-
/// That is, `random_ratio(2, 3)` has chance of 2 in 3, or about 67%, of
201+
/// That is, `random_probability(2, 3)` has chance of 2 in 3, or about 67%, of
202202
/// returning true. If `numerator == denominator`, then the returned value
203203
/// is guaranteed to be `true`. If `numerator == 0`, then the returned
204204
/// value is guaranteed to be `false`.
@@ -216,13 +216,13 @@ pub trait Rng: RngCore {
216216
/// use rand::Rng;
217217
///
218218
/// let mut rng = rand::rng();
219-
/// println!("{}", rng.random_ratio(2, 3));
219+
/// println!("{}", rng.random_probability(2, 3));
220220
/// ```
221221
///
222222
/// [`Bernoulli`]: distr::Bernoulli
223223
#[inline]
224224
#[track_caller]
225-
fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool {
225+
fn random_probability(&mut self, numerator: u32, denominator: u32) -> bool {
226226
match distr::Bernoulli::from_ratio(numerator, denominator) {
227227
Ok(d) => self.sample(d),
228228
Err(_) => panic!(
@@ -339,18 +339,18 @@ pub trait Rng: RngCore {
339339
self.random_range(range)
340340
}
341341

342-
/// Alias for [`Rng::random_bool`].
342+
/// Alias for [`Rng::random_probability_f64`].
343343
#[inline]
344-
#[deprecated(since = "0.9.0", note = "Renamed to `random_bool`")]
344+
#[deprecated(since = "0.9.0", note = "Renamed to `random_probability_f64`")]
345345
fn gen_bool(&mut self, p: f64) -> bool {
346-
self.random_bool(p)
346+
self.random_probability_f64(p)
347347
}
348348

349-
/// Alias for [`Rng::random_ratio`].
349+
/// Alias for [`Rng::random_probability`].
350350
#[inline]
351-
#[deprecated(since = "0.9.0", note = "Renamed to `random_ratio`")]
351+
#[deprecated(since = "0.9.0", note = "Renamed to `random_probability`")]
352352
fn gen_ratio(&mut self, numerator: u32, denominator: u32) -> bool {
353-
self.random_ratio(numerator, denominator)
353+
self.random_probability(numerator, denominator)
354354
}
355355
}
356356

@@ -560,11 +560,11 @@ mod test {
560560

561561
#[test]
562562
#[allow(clippy::bool_assert_comparison)]
563-
fn test_random_bool() {
563+
fn test_random_probability_f64() {
564564
let mut r = rng(105);
565565
for _ in 0..5 {
566-
assert_eq!(r.random_bool(0.0), false);
567-
assert_eq!(r.random_bool(1.0), true);
566+
assert_eq!(r.random_probability_f64(0.0), false);
567+
assert_eq!(r.random_probability_f64(1.0), true);
568568
}
569569
}
570570

@@ -611,7 +611,7 @@ mod test {
611611
let mut sum: u32 = 0;
612612
let mut rng = rng(111);
613613
for _ in 0..N {
614-
if rng.random_ratio(NUM, DENOM) {
614+
if rng.random_probability(NUM, DENOM) {
615615
sum += 1;
616616
}
617617
}

src/seq/coin_flipper.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ impl<R: RngCore> CoinFlipper<R> {
2727
/// Returns true with a probability of 1 / d
2828
/// Uses an expected two bits of randomness
2929
/// Panics if d == 0
30-
pub fn random_ratio_one_over(&mut self, d: usize) -> bool {
30+
pub fn random_probability_one_over(&mut self, d: usize) -> bool {
3131
debug_assert_ne!(d, 0);
32-
// This uses the same logic as `random_ratio` but is optimized for the case that
32+
// This uses the same logic as `random_probability` but is optimized for the case that
3333
// the starting numerator is one (which it always is for `Sequence::Choose()`)
3434

35-
// In this case (but not `random_ratio`), this way of calculating c is always accurate
35+
// In this case (but not `random_probability`), this way of calculating c is always accurate
3636
let c = (usize::BITS - 1 - d.leading_zeros()).min(32);
3737

3838
if self.flip_c_heads(c) {
3939
let numerator = 1 << c;
40-
self.random_ratio(numerator, d)
40+
self.random_probability(numerator, d)
4141
} else {
4242
false
4343
}
@@ -46,7 +46,7 @@ impl<R: RngCore> CoinFlipper<R> {
4646
#[inline]
4747
/// Returns true with a probability of n / d
4848
/// Uses an expected two bits of randomness
49-
fn random_ratio(&mut self, mut n: usize, d: usize) -> bool {
49+
fn random_probability(&mut self, mut n: usize, d: usize) -> bool {
5050
// Explanation:
5151
// We are trying to return true with a probability of n / d
5252
// If n >= d, we can just return true

src/seq/iterator.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub trait IteratorRandom: Iterator + Sized {
107107
return result;
108108
}
109109
consumed += 1;
110-
if coin_flipper.random_ratio_one_over(consumed) {
110+
if coin_flipper.random_probability_one_over(consumed) {
111111
result = elem;
112112
}
113113
}
@@ -152,7 +152,7 @@ pub trait IteratorRandom: Iterator + Sized {
152152
let (lower, _) = self.size_hint();
153153
if lower >= 2 {
154154
let highest_selected = (0..lower)
155-
.filter(|ix| coin_flipper.random_ratio_one_over(consumed + ix + 1))
155+
.filter(|ix| coin_flipper.random_probability_one_over(consumed + ix + 1))
156156
.last();
157157

158158
consumed += lower;
@@ -170,7 +170,7 @@ pub trait IteratorRandom: Iterator + Sized {
170170
return result;
171171
}
172172

173-
if coin_flipper.random_ratio_one_over(consumed + 1) {
173+
if coin_flipper.random_probability_one_over(consumed + 1) {
174174
result = elem;
175175
}
176176
consumed += 1;

0 commit comments

Comments
 (0)