Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rng renames: gen_ → random_ #1505

Merged
merged 9 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
- Rename `Rng::gen_iter` to `random_iter` (#1500)
- Rename `rand::thread_rng()` to `rand::rng()`, and remove from the prelude (#1506)
- Remove `rand::random()` from the prelude (#1506)
- Rename `Rng::gen_range` to `random_range`, `gen_bool` to `random_bool`, `gen_ratio` to `random_ratio` (#1505)

## [0.9.0-alpha.1] - 2024-03-18
- Add the `Slice::num_choices` method to the Slice distribution (#1402)
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ criterion_group!(
criterion_main!(benches);

pub fn bench(c: &mut Criterion) {
let mut g = c.benchmark_group("gen_1kb");
let mut g = c.benchmark_group("random_1kb");
g.throughput(criterion::Throughput::Bytes(1024));

g.bench_function("u16_iter_repeat", |b| {
Expand Down
14 changes: 7 additions & 7 deletions benches/benches/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ criterion_group!(
criterion_main!(benches);

pub fn bench(c: &mut Criterion) {
let mut g = c.benchmark_group("gen_bool");
let mut g = c.benchmark_group("random_bool");
g.sample_size(1000);
g.warm_up_time(core::time::Duration::from_millis(500));
g.measurement_time(core::time::Duration::from_millis(1000));
Expand All @@ -33,25 +33,25 @@ pub fn bench(c: &mut Criterion) {

g.bench_function("const", |b| {
let mut rng = Pcg32::from_rng(&mut rand::rng());
b.iter(|| rng.gen_bool(0.18))
b.iter(|| rng.random_bool(0.18))
});

g.bench_function("var", |b| {
let mut rng = Pcg32::from_rng(&mut rand::rng());
let p = rng.random();
b.iter(|| rng.gen_bool(p))
b.iter(|| rng.random_bool(p))
});

g.bench_function("ratio_const", |b| {
let mut rng = Pcg32::from_rng(&mut rand::rng());
b.iter(|| rng.gen_ratio(2, 3))
b.iter(|| rng.random_ratio(2, 3))
});

g.bench_function("ratio_var", |b| {
let mut rng = Pcg32::from_rng(&mut rand::rng());
let d = rng.gen_range(1..=100);
let n = rng.gen_range(0..=d);
b.iter(|| rng.gen_ratio(n, d));
let d = rng.random_range(1..=100);
let n = rng.random_range(0..=d);
b.iter(|| rng.random_ratio(n, d));
});

g.bench_function("bernoulli_const", |b| {
Expand Down
14 changes: 7 additions & 7 deletions benches/benches/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ use rand_pcg::{Pcg32, Pcg64, Pcg64Dxsm, Pcg64Mcg};
criterion_group!(
name = benches;
config = Criterion::default();
targets = gen_bytes, gen_u32, gen_u64, init_gen, init_from_u64, init_from_seed, reseeding_bytes
targets = random_bytes, random_u32, random_u64, init_gen, init_from_u64, init_from_seed, reseeding_bytes
);
criterion_main!(benches);

pub fn gen_bytes(c: &mut Criterion) {
let mut g = c.benchmark_group("gen_bytes");
pub fn random_bytes(c: &mut Criterion) {
let mut g = c.benchmark_group("random_bytes");
g.warm_up_time(Duration::from_millis(500));
g.measurement_time(Duration::from_millis(1000));
g.throughput(criterion::Throughput::Bytes(1024));
Expand Down Expand Up @@ -55,8 +55,8 @@ pub fn gen_bytes(c: &mut Criterion) {
g.finish()
}

pub fn gen_u32(c: &mut Criterion) {
let mut g = c.benchmark_group("gen_u32");
pub fn random_u32(c: &mut Criterion) {
let mut g = c.benchmark_group("random_u32");
g.sample_size(1000);
g.warm_up_time(Duration::from_millis(500));
g.measurement_time(Duration::from_millis(1000));
Expand Down Expand Up @@ -84,8 +84,8 @@ pub fn gen_u32(c: &mut Criterion) {
g.finish()
}

pub fn gen_u64(c: &mut Criterion) {
let mut g = c.benchmark_group("gen_u64");
pub fn random_u64(c: &mut Criterion) {
let mut g = c.benchmark_group("random_u64");
g.sample_size(1000);
g.warm_up_time(Duration::from_millis(500));
g.measurement_time(Duration::from_millis(1000));
Expand Down
2 changes: 1 addition & 1 deletion rand_distr/src/weighted_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl<W: Clone + PartialEq + PartialOrd + SampleUniform + SubAssign<W> + Weight>
if total_weight == W::ZERO {
return Err(WeightError::InsufficientNonZero);
}
let mut target_weight = rng.gen_range(W::ZERO..total_weight);
let mut target_weight = rng.random_range(W::ZERO..total_weight);
let mut index = 0;
loop {
// Maybe descend into the left sub tree.
Expand Down
4 changes: 2 additions & 2 deletions src/distr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
//! # Non-uniform sampling
//!
//! Sampling a simple true/false outcome with a given probability has a name:
//! the [`Bernoulli`] distribution (this is used by [`Rng::gen_bool`]).
//! the [`Bernoulli`] distribution (this is used by [`Rng::random_bool`]).
//!
//! For weighted sampling from a sequence of discrete values, use the
//! [`WeightedIndex`] distribution.
Expand Down Expand Up @@ -204,7 +204,7 @@ use crate::Rng;
/// multiplicative method: `(rng.gen::<$uty>() >> N) as $ty * (ε/2)`.
///
/// See also: [`Open01`] which samples from `(0, 1)`, [`OpenClosed01`] which
/// samples from `(0, 1]` and `Rng::gen_range(0..1)` which also samples from
/// samples from `(0, 1]` and `Rng::random_range(0..1)` which also samples from
/// `[0, 1)`. Note that `Open01` uses transmute-based methods which yield 1 bit
/// less precision but may perform faster on some architectures (on modern Intel
/// CPUs all methods have approximately equal performance).
Expand Down
14 changes: 7 additions & 7 deletions src/distr/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//!
//! [`Uniform`] is the standard distribution to sample uniformly from a range;
//! e.g. `Uniform::new_inclusive(1, 6).unwrap()` can sample integers from 1 to 6, like a
//! standard die. [`Rng::gen_range`] supports any type supported by [`Uniform`].
//! standard die. [`Rng::random_range`] supports any type supported by [`Uniform`].
//!
//! This distribution is provided with support for several primitive types
//! (all integer and floating-point types) as well as [`std::time::Duration`],
Expand All @@ -33,7 +33,7 @@
//! let side = Uniform::new(-10.0, 10.0).unwrap();
//!
//! // sample between 1 and 10 points
//! for _ in 0..rng.gen_range(1..=10) {
//! for _ in 0..rng.random_range(1..=10) {
//! // sample a point from the square with sides -10 - 10 in two dimensions
//! let (x, y) = (rng.sample(side), rng.sample(side));
//! println!("Point: {}, {}", x, y);
Expand Down Expand Up @@ -154,7 +154,7 @@ use serde::{Deserialize, Serialize};
/// [`Uniform::new`] and [`Uniform::new_inclusive`] construct a uniform
/// distribution sampling from the given range; these functions may do extra
/// work up front to make sampling of multiple values faster. If only one sample
/// from the range is required, [`Rng::gen_range`] can be more efficient.
/// from the range is required, [`Rng::random_range`] can be more efficient.
///
/// When sampling from a constant range, many calculations can happen at
/// compile-time and all methods should be fast; for floating-point ranges and
Expand Down Expand Up @@ -186,18 +186,18 @@ use serde::{Deserialize, Serialize};
/// println!("{}", sum);
/// ```
///
/// For a single sample, [`Rng::gen_range`] may be preferred:
/// For a single sample, [`Rng::random_range`] may be preferred:
///
/// ```
/// use rand::Rng;
///
/// let mut rng = rand::rng();
/// println!("{}", rng.gen_range(0..10));
/// println!("{}", rng.random_range(0..10));
/// ```
///
/// [`new`]: Uniform::new
/// [`new_inclusive`]: Uniform::new_inclusive
/// [`Rng::gen_range`]: Rng::gen_range
/// [`Rng::random_range`]: Rng::random_range
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(bound(serialize = "X::Sampler: Serialize")))]
Expand Down Expand Up @@ -406,7 +406,7 @@ where
/// Range that supports generating a single sample efficiently.
///
/// Any type implementing this trait can be used to specify the sampled range
/// for `Rng::gen_range`.
/// for `Rng::random_range`.
pub trait SampleRange<T> {
/// Generate a sample from the given range.
fn sample_single<R: RngCore + ?Sized>(self, rng: &mut R) -> Result<T, Error>;
Expand Down
2 changes: 1 addition & 1 deletion src/distr/uniform_float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ mod tests {
#[should_panic]
fn test_float_overflow_single() {
let mut rng = crate::test::rng(252);
rng.gen_range(f64::MIN..f64::MAX);
rng.random_range(f64::MIN..f64::MAX);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/distr/uniform_other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ mod tests {
let mut rng = crate::test::rng(891);
let mut max = core::char::from_u32(0).unwrap();
for _ in 0..100 {
let c = rng.gen_range('A'..='Z');
let c = rng.random_range('A'..='Z');
assert!(c.is_ascii_uppercase());
max = max.max(c);
}
Expand Down
Loading