Skip to content

Commit 8225d94

Browse files
authored
Rng renames: gen_ → random_ (#1505)
1 parent 9d57b87 commit 8225d94

17 files changed

Lines changed: 194 additions & 168 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
3030
- Rename `Rng::gen_iter` to `random_iter` (#1500)
3131
- Rename `rand::thread_rng()` to `rand::rng()`, and remove from the prelude (#1506)
3232
- Remove `rand::random()` from the prelude (#1506)
33+
- Rename `Rng::gen_range` to `random_range`, `gen_bool` to `random_bool`, `gen_ratio` to `random_ratio` (#1505)
3334

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

benches/benches/array.rs

Lines changed: 1 addition & 1 deletion
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("gen_1kb");
24+
let mut g = c.benchmark_group("random_1kb");
2525
g.throughput(criterion::Throughput::Bytes(1024));
2626

2727
g.bench_function("u16_iter_repeat", |b| {

benches/benches/bool.rs

Lines changed: 7 additions & 7 deletions
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("gen_bool");
24+
let mut g = c.benchmark_group("random_bool");
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.gen_bool(0.18))
36+
b.iter(|| rng.random_bool(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.gen_bool(p))
42+
b.iter(|| rng.random_bool(p))
4343
});
4444

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

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

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

benches/benches/generators.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ use rand_pcg::{Pcg32, Pcg64, Pcg64Dxsm, Pcg64Mcg};
1919
criterion_group!(
2020
name = benches;
2121
config = Criterion::default();
22-
targets = gen_bytes, gen_u32, gen_u64, init_gen, init_from_u64, init_from_seed, reseeding_bytes
22+
targets = random_bytes, random_u32, random_u64, init_gen, init_from_u64, init_from_seed, reseeding_bytes
2323
);
2424
criterion_main!(benches);
2525

26-
pub fn gen_bytes(c: &mut Criterion) {
27-
let mut g = c.benchmark_group("gen_bytes");
26+
pub fn random_bytes(c: &mut Criterion) {
27+
let mut g = c.benchmark_group("random_bytes");
2828
g.warm_up_time(Duration::from_millis(500));
2929
g.measurement_time(Duration::from_millis(1000));
3030
g.throughput(criterion::Throughput::Bytes(1024));
@@ -55,8 +55,8 @@ pub fn gen_bytes(c: &mut Criterion) {
5555
g.finish()
5656
}
5757

58-
pub fn gen_u32(c: &mut Criterion) {
59-
let mut g = c.benchmark_group("gen_u32");
58+
pub fn random_u32(c: &mut Criterion) {
59+
let mut g = c.benchmark_group("random_u32");
6060
g.sample_size(1000);
6161
g.warm_up_time(Duration::from_millis(500));
6262
g.measurement_time(Duration::from_millis(1000));
@@ -84,8 +84,8 @@ pub fn gen_u32(c: &mut Criterion) {
8484
g.finish()
8585
}
8686

87-
pub fn gen_u64(c: &mut Criterion) {
88-
let mut g = c.benchmark_group("gen_u64");
87+
pub fn random_u64(c: &mut Criterion) {
88+
let mut g = c.benchmark_group("random_u64");
8989
g.sample_size(1000);
9090
g.warm_up_time(Duration::from_millis(500));
9191
g.measurement_time(Duration::from_millis(1000));

rand_distr/src/weighted_tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ impl<W: Clone + PartialEq + PartialOrd + SampleUniform + SubAssign<W> + Weight>
251251
if total_weight == W::ZERO {
252252
return Err(WeightError::InsufficientNonZero);
253253
}
254-
let mut target_weight = rng.gen_range(W::ZERO..total_weight);
254+
let mut target_weight = rng.random_range(W::ZERO..total_weight);
255255
let mut index = 0;
256256
loop {
257257
// Maybe descend into the left sub tree.

src/distr/mod.rs

Lines changed: 2 additions & 2 deletions
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::gen_bool`]).
79+
//! the [`Bernoulli`] distribution (this is used by [`Rng::random_bool`]).
8080
//!
8181
//! For weighted sampling from a sequence of discrete values, use the
8282
//! [`WeightedIndex`] distribution.
@@ -204,7 +204,7 @@ use crate::Rng;
204204
/// multiplicative method: `(rng.gen::<$uty>() >> N) as $ty * (ε/2)`.
205205
///
206206
/// See also: [`Open01`] which samples from `(0, 1)`, [`OpenClosed01`] which
207-
/// samples from `(0, 1]` and `Rng::gen_range(0..1)` which also samples from
207+
/// samples from `(0, 1]` and `Rng::random_range(0..1)` which also samples from
208208
/// `[0, 1)`. Note that `Open01` uses transmute-based methods which yield 1 bit
209209
/// less precision but may perform faster on some architectures (on modern Intel
210210
/// CPUs all methods have approximately equal performance).

src/distr/uniform.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//!
1212
//! [`Uniform`] is the standard distribution to sample uniformly from a range;
1313
//! e.g. `Uniform::new_inclusive(1, 6).unwrap()` can sample integers from 1 to 6, like a
14-
//! standard die. [`Rng::gen_range`] supports any type supported by [`Uniform`].
14+
//! standard die. [`Rng::random_range`] supports any type supported by [`Uniform`].
1515
//!
1616
//! This distribution is provided with support for several primitive types
1717
//! (all integer and floating-point types) as well as [`std::time::Duration`],
@@ -33,7 +33,7 @@
3333
//! let side = Uniform::new(-10.0, 10.0).unwrap();
3434
//!
3535
//! // sample between 1 and 10 points
36-
//! for _ in 0..rng.gen_range(1..=10) {
36+
//! for _ in 0..rng.random_range(1..=10) {
3737
//! // sample a point from the square with sides -10 - 10 in two dimensions
3838
//! let (x, y) = (rng.sample(side), rng.sample(side));
3939
//! println!("Point: {}, {}", x, y);
@@ -154,7 +154,7 @@ use serde::{Deserialize, Serialize};
154154
/// [`Uniform::new`] and [`Uniform::new_inclusive`] construct a uniform
155155
/// distribution sampling from the given range; these functions may do extra
156156
/// work up front to make sampling of multiple values faster. If only one sample
157-
/// from the range is required, [`Rng::gen_range`] can be more efficient.
157+
/// from the range is required, [`Rng::random_range`] can be more efficient.
158158
///
159159
/// When sampling from a constant range, many calculations can happen at
160160
/// compile-time and all methods should be fast; for floating-point ranges and
@@ -186,18 +186,18 @@ use serde::{Deserialize, Serialize};
186186
/// println!("{}", sum);
187187
/// ```
188188
///
189-
/// For a single sample, [`Rng::gen_range`] may be preferred:
189+
/// For a single sample, [`Rng::random_range`] may be preferred:
190190
///
191191
/// ```
192192
/// use rand::Rng;
193193
///
194194
/// let mut rng = rand::rng();
195-
/// println!("{}", rng.gen_range(0..10));
195+
/// println!("{}", rng.random_range(0..10));
196196
/// ```
197197
///
198198
/// [`new`]: Uniform::new
199199
/// [`new_inclusive`]: Uniform::new_inclusive
200-
/// [`Rng::gen_range`]: Rng::gen_range
200+
/// [`Rng::random_range`]: Rng::random_range
201201
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
202202
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
203203
#[cfg_attr(feature = "serde", serde(bound(serialize = "X::Sampler: Serialize")))]
@@ -406,7 +406,7 @@ where
406406
/// Range that supports generating a single sample efficiently.
407407
///
408408
/// Any type implementing this trait can be used to specify the sampled range
409-
/// for `Rng::gen_range`.
409+
/// for `Rng::random_range`.
410410
pub trait SampleRange<T> {
411411
/// Generate a sample from the given range.
412412
fn sample_single<R: RngCore + ?Sized>(self, rng: &mut R) -> Result<T, Error>;

src/distr/uniform_float.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ mod tests {
364364
#[should_panic]
365365
fn test_float_overflow_single() {
366366
let mut rng = crate::test::rng(252);
367-
rng.gen_range(f64::MIN..f64::MAX);
367+
rng.random_range(f64::MIN..f64::MAX);
368368
}
369369

370370
#[test]

src/distr/uniform_other.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ mod tests {
265265
let mut rng = crate::test::rng(891);
266266
let mut max = core::char::from_u32(0).unwrap();
267267
for _ in 0..100 {
268-
let c = rng.gen_range('A'..='Z');
268+
let c = rng.random_range('A'..='Z');
269269
assert!(c.is_ascii_uppercase());
270270
max = max.max(c);
271271
}

0 commit comments

Comments
 (0)