Skip to content

Commit

Permalink
Rename Rng::gen to Rng::random
Browse files Browse the repository at this point in the history
This anticipates `gen` being a keyword in Rust 2024. A deprecated alias
for `Rng::gen` stays in place to make the transition easier.

Fixes rust-random#1435.
  • Loading branch information
vks committed Apr 26, 2024
1 parent d42daab commit bd304d2
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 64 deletions.
8 changes: 4 additions & 4 deletions benches/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn gen_1kb_u16_iter_repeat(b: &mut Bencher) {
use core::iter;
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
let v: Vec<u16> = iter::repeat(()).map(|()| rng.gen()).take(512).collect();
let v: Vec<u16> = iter::repeat(()).map(|()| rng.random()).take(512).collect();
v
});
b.bytes = 1024;
Expand All @@ -122,7 +122,7 @@ fn gen_1kb_u16_gen_array(b: &mut Bencher) {
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
// max supported array length is 32!
let v: [[u16; 32]; 16] = rng.gen();
let v: [[u16; 32]; 16] = rng.random();
v
});
b.bytes = 1024;
Expand All @@ -144,7 +144,7 @@ fn gen_1kb_u64_iter_repeat(b: &mut Bencher) {
use core::iter;
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
let v: Vec<u64> = iter::repeat(()).map(|()| rng.gen()).take(128).collect();
let v: Vec<u64> = iter::repeat(()).map(|()| rng.random()).take(128).collect();
v
});
b.bytes = 1024;
Expand All @@ -165,7 +165,7 @@ fn gen_1kb_u64_gen_array(b: &mut Bencher) {
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
// max supported array length is 32!
let v: [[u64; 32]; 4] = rng.gen();
let v: [[u64; 32]; 4] = rng.random();
v
});
b.bytes = 1024;
Expand Down
2 changes: 1 addition & 1 deletion examples/monty-hall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn simulate<R: Rng>(random_door: &Uniform<u32>, rng: &mut R) -> SimulationResult
let open = game_host_open(car, choice, rng);

// Shall we switch?
let switch = rng.gen();
let switch = rng.random();
if switch {
choice = switch_door(choice, open);
}
Expand Down
2 changes: 1 addition & 1 deletion src/distributions/bernoulli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl Distribution<bool> for Bernoulli {
if self.p_int == ALWAYS_TRUE {
return true;
}
let v: u64 = rng.gen();
let v: u64 = rng.random();
v < self.p_int
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/distributions/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ macro_rules! float_impls {
let precision = $fraction_bits + 1;
let scale = 1.0 / ((1 as $u_scalar << precision) as $f_scalar);

let value: $uty = rng.gen();
let value: $uty = rng.random();
let value = value >> $uty::splat(float_size - precision);
$ty::splat(scale) * $ty::cast_from_int(value)
}
Expand All @@ -132,7 +132,7 @@ macro_rules! float_impls {
let precision = $fraction_bits + 1;
let scale = 1.0 / ((1 as $u_scalar << precision) as $f_scalar);

let value: $uty = rng.gen();
let value: $uty = rng.random();
let value = value >> $uty::splat(float_size - precision);
// Add 1 to shift up; will not overflow because of right-shift:
$ty::splat(scale) * $ty::cast_from_int(value + $uty::splat(1))
Expand All @@ -149,7 +149,7 @@ macro_rules! float_impls {
use core::$f_scalar::EPSILON;
let float_size = mem::size_of::<$f_scalar>() as $u_scalar * 8;

let value: $uty = rng.gen();
let value: $uty = rng.random();
let fraction = value >> $uty::splat(float_size - $fraction_bits);
fraction.into_float_with_exponent(0) - $ty::splat(1.0 - EPSILON / 2.0)
}
Expand Down Expand Up @@ -192,11 +192,11 @@ mod tests {

// Standard
let mut zeros = StepRng::new(0, 0);
assert_eq!(zeros.gen::<$ty>(), $ZERO);
assert_eq!(zeros.random::<$ty>(), $ZERO);
let mut one = StepRng::new(1 << 8 | 1 << (8 + 32), 0);
assert_eq!(one.gen::<$ty>(), $EPSILON / two);
assert_eq!(one.random::<$ty>(), $EPSILON / two);
let mut max = StepRng::new(!0, 0);
assert_eq!(max.gen::<$ty>(), $ty::splat(1.0) - $EPSILON / two);
assert_eq!(max.random::<$ty>(), $ty::splat(1.0) - $EPSILON / two);

// OpenClosed01
let mut zeros = StepRng::new(0, 0);
Expand Down Expand Up @@ -234,11 +234,11 @@ mod tests {

// Standard
let mut zeros = StepRng::new(0, 0);
assert_eq!(zeros.gen::<$ty>(), $ZERO);
assert_eq!(zeros.random::<$ty>(), $ZERO);
let mut one = StepRng::new(1 << 11, 0);
assert_eq!(one.gen::<$ty>(), $EPSILON / two);
assert_eq!(one.random::<$ty>(), $EPSILON / two);
let mut max = StepRng::new(!0, 0);
assert_eq!(max.gen::<$ty>(), $ty::splat(1.0) - $EPSILON / two);
assert_eq!(max.random::<$ty>(), $ty::splat(1.0) - $EPSILON / two);

// OpenClosed01
let mut zeros = StepRng::new(0, 0);
Expand Down
4 changes: 2 additions & 2 deletions src/distributions/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ macro_rules! impl_int_from_uint {
impl Distribution<$ty> for Standard {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $ty {
rng.gen::<$uty>() as $ty
rng.random::<$uty>() as $ty
}
}
};
Expand All @@ -99,7 +99,7 @@ macro_rules! impl_nzint {
impl Distribution<$ty> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $ty {
loop {
if let Some(nz) = $new(rng.gen()) {
if let Some(nz) = $new(rng.random()) {
break nz;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/distributions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//!
//! This module is the home of the [`Distribution`] trait and several of its
//! implementations. It is the workhorse behind some of the convenient
//! functionality of the [`Rng`] trait, e.g. [`Rng::gen`] and of course
//! functionality of the [`Rng`] trait, e.g. [`Rng::random`] and of course
//! [`Rng::sample`].
//!
//! Abstractly, a [probability distribution] describes the probability of
Expand All @@ -31,13 +31,13 @@
//! # The `Standard` distribution
//!
//! The [`Standard`] distribution is important to mention. This is the
//! distribution used by [`Rng::gen`] and represents the "default" way to
//! distribution used by [`Rng::random`] and represents the "default" way to
//! produce a random value for many different types, including most primitive
//! types, tuples, arrays, and a few derived types. See the documentation of
//! [`Standard`] for more details.
//!
//! Implementing `Distribution<T>` for [`Standard`] for user types `T` makes it
//! possible to generate type `T` with [`Rng::gen`], and by extension also
//! possible to generate type `T` with [`Rng::random`], and by extension also
//! with the [`random`] function.
//!
//! ## Random characters
Expand Down Expand Up @@ -181,7 +181,7 @@ use crate::Rng;
///
/// impl Distribution<MyF32> for Standard {
/// fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> MyF32 {
/// MyF32 { x: rng.gen() }
/// MyF32 { x: rng.random() }
/// }
/// }
/// ```
Expand Down
14 changes: 7 additions & 7 deletions src/distributions/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ where
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Mask<T, LANES> {
// `MaskElement` must be a signed integer, so this is equivalent
// to the scalar `i32 < 0` method
let var = rng.gen::<Simd<T, LANES>>();
let var = rng.random::<Simd<T, LANES>>();
var.simd_lt(Simd::default())
}
}
Expand All @@ -208,7 +208,7 @@ macro_rules! tuple_impl {
let out = ($(
// use the $tyvar's to get the appropriate number of
// repeats (they're not actually needed)
rng.gen::<$tyvar>()
rng.random::<$tyvar>()
,)*);

// Suppress the unused variable warning for empty tuple
Expand Down Expand Up @@ -247,7 +247,7 @@ where Standard: Distribution<T>
let mut buff: [MaybeUninit<T>; N] = unsafe { MaybeUninit::uninit().assume_init() };

for elem in &mut buff {
*elem = MaybeUninit::new(_rng.gen());
*elem = MaybeUninit::new(_rng.random());
}

unsafe { mem::transmute_copy::<_, _>(&buff) }
Expand All @@ -260,8 +260,8 @@ where Standard: Distribution<T>
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Option<T> {
// UFCS is needed here: https://github.com/rust-lang/rust/issues/24066
if rng.gen::<bool>() {
Some(rng.gen())
if rng.random::<bool>() {
Some(rng.random())
} else {
None
}
Expand All @@ -273,7 +273,7 @@ where Standard: Distribution<T>
{
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Wrapping<T> {
Wrapping(rng.gen())
Wrapping(rng.random())
}
}

Expand All @@ -300,7 +300,7 @@ mod tests {
// Test by generating a relatively large number of chars, so we also
// take the rejection sampling path.
let word: String = iter::repeat(())
.map(|()| rng.gen::<char>())
.map(|()| rng.random::<char>())
.take(1000)
.collect();
assert!(!word.is_empty());
Expand Down
24 changes: 12 additions & 12 deletions src/distributions/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,12 +515,12 @@ macro_rules! uniform_int_impl {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::X {
let range = self.range as $uty as $sample_ty;
if range == 0 {
return rng.gen();
return rng.random();
}

let thresh = self.thresh as $uty as $sample_ty;
let hi = loop {
let (hi, lo) = rng.gen::<$sample_ty>().wmul(range);
let (hi, lo) = rng.random::<$sample_ty>().wmul(range);
if lo >= thresh {
break hi;
}
Expand Down Expand Up @@ -563,16 +563,16 @@ macro_rules! uniform_int_impl {
let range = high.wrapping_sub(low).wrapping_add(1) as $uty as $sample_ty;
if range == 0 {
// Range is MAX+1 (unrepresentable), so we need a special case
return Ok(rng.gen());
return Ok(rng.random());
}

// generate a sample using a sensible integer type
let (mut result, lo_order) = rng.gen::<$sample_ty>().wmul(range);
let (mut result, lo_order) = rng.random::<$sample_ty>().wmul(range);

// if the sample is biased...
if lo_order > range.wrapping_neg() {
// ...generate a new sample to reduce bias...
let (new_hi_order, _) = (rng.gen::<$sample_ty>()).wmul(range as $sample_ty);
let (new_hi_order, _) = (rng.random::<$sample_ty>()).wmul(range as $sample_ty);
// ... incrementing result on overflow
let is_overflow = lo_order.checked_add(new_hi_order as $sample_ty).is_none();
result += is_overflow as $sample_ty;
Expand Down Expand Up @@ -602,11 +602,11 @@ macro_rules! uniform_int_impl {
return Ok(rng.gen());
}

let (mut result, mut lo) = rng.gen::<$sample_ty>().wmul(range);
let (mut result, mut lo) = rng.random::<$sample_ty>().wmul(range);

// In contrast to the biased sampler, we use a loop:
while lo > range.wrapping_neg() {
let (new_hi, new_lo) = (rng.gen::<$sample_ty>()).wmul(range);
let (new_hi, new_lo) = (rng.random::<$sample_ty>()).wmul(range);
match lo.checked_add(new_hi) {
Some(x) if x < $sample_ty::MAX => {
// Anything less than MAX: last term is 0
Expand Down Expand Up @@ -732,7 +732,7 @@ macro_rules! uniform_simd_int_impl {
// rejection. The replacement method does however add a little
// overhead. Benchmarking or calculating probabilities might
// reveal contexts where this replacement method is slower.
let mut v: Simd<$unsigned, LANES> = rng.gen();
let mut v: Simd<$unsigned, LANES> = rng.random();
loop {
let (hi, lo) = v.wmul(range);
let mask = lo.simd_ge(thresh);
Expand All @@ -747,7 +747,7 @@ macro_rules! uniform_simd_int_impl {
return range.simd_gt(Simd::splat(0)).select(result, v);
}
// Replace only the failing lanes
v = mask.select(v, rng.gen());
v = mask.select(v, rng.random());
}
}
}
Expand Down Expand Up @@ -970,7 +970,7 @@ macro_rules! uniform_float_impl {

fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::X {
// Generate a value in the range [1, 2)
let value1_2 = (rng.gen::<$uty>() >> $uty::splat($bits_to_discard)).into_float_with_exponent(0);
let value1_2 = (rng.random::<$uty>() >> $uty::splat($bits_to_discard)).into_float_with_exponent(0);

// Get a value in the range [0, 1) to avoid overflow when multiplying by scale
let value0_1 = value1_2 - <$ty>::splat(1.0);
Expand Down Expand Up @@ -1006,7 +1006,7 @@ macro_rules! uniform_float_impl {
loop {
// Generate a value in the range [1, 2)
let value1_2 =
(rng.gen::<$uty>() >> $uty::splat($bits_to_discard)).into_float_with_exponent(0);
(rng.random::<$uty>() >> $uty::splat($bits_to_discard)).into_float_with_exponent(0);

// Get a value in the range [0, 1) to avoid overflow when multiplying by scale
let value0_1 = value1_2 - <$ty>::splat(1.0);
Expand Down Expand Up @@ -1079,7 +1079,7 @@ macro_rules! uniform_float_impl {

// Generate a value in the range [1, 2)
let value1_2 =
(rng.gen::<$uty>() >> $uty::splat($bits_to_discard)).into_float_with_exponent(0);
(rng.random::<$uty>() >> $uty::splat($bits_to_discard)).into_float_with_exponent(0);

// Get a value in the range [0, 1) to avoid overflow when multiplying by scale
let value0_1 = value1_2 - <$ty>::splat(1.0);
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
//! }
//!
//! let mut rng = rand::thread_rng();
//! let y: f64 = rng.gen(); // generates a float between 0 and 1
//! let y: f64 = rng.random(); // generates a float between 0 and 1
//!
//! let mut nums: Vec<i32> = (1..100).collect();
//! nums.shuffle(&mut rng);
Expand Down Expand Up @@ -147,7 +147,7 @@ use crate::distributions::{Distribution, Standard};
/// let mut rng = rand::thread_rng();
///
/// for x in v.iter_mut() {
/// *x = rng.gen();
/// *x = rng.random();
/// }
/// ```
///
Expand All @@ -158,7 +158,7 @@ use crate::distributions::{Distribution, Standard};
#[inline]
pub fn random<T>() -> T
where Standard: Distribution<T> {
thread_rng().gen()
thread_rng().random()
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//! ```
//! use rand::prelude::*;
//! # let mut r = StdRng::from_rng(thread_rng()).unwrap();
//! # let _: f32 = r.gen();
//! # let _: f32 = r.random();
//! ```
#[doc(no_inline)] pub use crate::distributions::Distribution;
Expand Down
Loading

0 comments on commit bd304d2

Please sign in to comment.