Skip to content

Commit 86262ac

Browse files
authored
Deprecate rand::rngs::mock module and StepRng (#1634)
1 parent a6e217f commit 86262ac

File tree

9 files changed

+72
-68
lines changed

9 files changed

+72
-68
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ A [separate changelog is kept for rand_core](rand_core/CHANGELOG.md).
88

99
You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.html) useful.
1010

11+
## [Unreleased]
12+
### Deprecated
13+
- Deprecate `rand::rngs::mock` module and `StepRng` generator (#1634)
14+
1115
## [0.9.1] - 2025-04-17
1216
### Security and unsafe
1317
- Revise "not a crypto library" policy again (#1565)

benches/benches/generators.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use core::time::Duration;
1010
use criterion::measurement::WallTime;
1111
use criterion::{black_box, criterion_group, criterion_main, BenchmarkGroup, Criterion};
1212
use rand::prelude::*;
13+
use rand::rngs::OsRng;
1314
use rand::rngs::ReseedingRng;
14-
use rand::rngs::{mock::StepRng, OsRng};
1515
use rand_chacha::rand_core::UnwrapErr;
1616
use rand_chacha::{ChaCha12Rng, ChaCha20Core, ChaCha20Rng, ChaCha8Rng};
1717
use rand_pcg::{Pcg32, Pcg64, Pcg64Dxsm, Pcg64Mcg};
@@ -39,7 +39,6 @@ pub fn random_bytes(c: &mut Criterion) {
3939
});
4040
}
4141

42-
bench(&mut g, "step", StepRng::new(0, 1));
4342
bench(&mut g, "pcg32", Pcg32::from_rng(&mut rand::rng()));
4443
bench(&mut g, "pcg64", Pcg64::from_rng(&mut rand::rng()));
4544
bench(&mut g, "pcg64mcg", Pcg64Mcg::from_rng(&mut rand::rng()));
@@ -68,7 +67,6 @@ pub fn random_u32(c: &mut Criterion) {
6867
});
6968
}
7069

71-
bench(&mut g, "step", StepRng::new(0, 1));
7270
bench(&mut g, "pcg32", Pcg32::from_rng(&mut rand::rng()));
7371
bench(&mut g, "pcg64", Pcg64::from_rng(&mut rand::rng()));
7472
bench(&mut g, "pcg64mcg", Pcg64Mcg::from_rng(&mut rand::rng()));
@@ -97,7 +95,6 @@ pub fn random_u64(c: &mut Criterion) {
9795
});
9896
}
9997

100-
bench(&mut g, "step", StepRng::new(0, 1));
10198
bench(&mut g, "pcg32", Pcg32::from_rng(&mut rand::rng()));
10299
bench(&mut g, "pcg64", Pcg64::from_rng(&mut rand::rng()));
103100
bench(&mut g, "pcg64mcg", Pcg64Mcg::from_rng(&mut rand::rng()));

src/distr/float.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ float_impls! { feature = "simd_support", f64x8, u64x8, f64, u64, 52, 1023 }
175175
#[cfg(test)]
176176
mod tests {
177177
use super::*;
178-
use crate::rngs::mock::StepRng;
178+
use crate::test::const_rng;
179179

180180
const EPSILON32: f32 = f32::EPSILON;
181181
const EPSILON64: f64 = f64::EPSILON;
@@ -187,30 +187,30 @@ mod tests {
187187
let two = $ty::splat(2.0);
188188

189189
// StandardUniform
190-
let mut zeros = StepRng::new(0, 0);
190+
let mut zeros = const_rng(0);
191191
assert_eq!(zeros.random::<$ty>(), $ZERO);
192-
let mut one = StepRng::new(1 << 8 | 1 << (8 + 32), 0);
192+
let mut one = const_rng(1 << 8 | 1 << (8 + 32));
193193
assert_eq!(one.random::<$ty>(), $EPSILON / two);
194-
let mut max = StepRng::new(!0, 0);
194+
let mut max = const_rng(!0);
195195
assert_eq!(max.random::<$ty>(), $ty::splat(1.0) - $EPSILON / two);
196196

197197
// OpenClosed01
198-
let mut zeros = StepRng::new(0, 0);
198+
let mut zeros = const_rng(0);
199199
assert_eq!(zeros.sample::<$ty, _>(OpenClosed01), $ZERO + $EPSILON / two);
200-
let mut one = StepRng::new(1 << 8 | 1 << (8 + 32), 0);
200+
let mut one = const_rng(1 << 8 | 1 << (8 + 32));
201201
assert_eq!(one.sample::<$ty, _>(OpenClosed01), $EPSILON);
202-
let mut max = StepRng::new(!0, 0);
202+
let mut max = const_rng(!0);
203203
assert_eq!(max.sample::<$ty, _>(OpenClosed01), $ZERO + $ty::splat(1.0));
204204

205205
// Open01
206-
let mut zeros = StepRng::new(0, 0);
206+
let mut zeros = const_rng(0);
207207
assert_eq!(zeros.sample::<$ty, _>(Open01), $ZERO + $EPSILON / two);
208-
let mut one = StepRng::new(1 << 9 | 1 << (9 + 32), 0);
208+
let mut one = const_rng(1 << 9 | 1 << (9 + 32));
209209
assert_eq!(
210210
one.sample::<$ty, _>(Open01),
211211
$EPSILON / two * $ty::splat(3.0)
212212
);
213-
let mut max = StepRng::new(!0, 0);
213+
let mut max = const_rng(!0);
214214
assert_eq!(
215215
max.sample::<$ty, _>(Open01),
216216
$ty::splat(1.0) - $EPSILON / two
@@ -235,30 +235,30 @@ mod tests {
235235
let two = $ty::splat(2.0);
236236

237237
// StandardUniform
238-
let mut zeros = StepRng::new(0, 0);
238+
let mut zeros = const_rng(0);
239239
assert_eq!(zeros.random::<$ty>(), $ZERO);
240-
let mut one = StepRng::new(1 << 11, 0);
240+
let mut one = const_rng(1 << 11);
241241
assert_eq!(one.random::<$ty>(), $EPSILON / two);
242-
let mut max = StepRng::new(!0, 0);
242+
let mut max = const_rng(!0);
243243
assert_eq!(max.random::<$ty>(), $ty::splat(1.0) - $EPSILON / two);
244244

245245
// OpenClosed01
246-
let mut zeros = StepRng::new(0, 0);
246+
let mut zeros = const_rng(0);
247247
assert_eq!(zeros.sample::<$ty, _>(OpenClosed01), $ZERO + $EPSILON / two);
248-
let mut one = StepRng::new(1 << 11, 0);
248+
let mut one = const_rng(1 << 11);
249249
assert_eq!(one.sample::<$ty, _>(OpenClosed01), $EPSILON);
250-
let mut max = StepRng::new(!0, 0);
250+
let mut max = const_rng(!0);
251251
assert_eq!(max.sample::<$ty, _>(OpenClosed01), $ZERO + $ty::splat(1.0));
252252

253253
// Open01
254-
let mut zeros = StepRng::new(0, 0);
254+
let mut zeros = const_rng(0);
255255
assert_eq!(zeros.sample::<$ty, _>(Open01), $ZERO + $EPSILON / two);
256-
let mut one = StepRng::new(1 << 12, 0);
256+
let mut one = const_rng(1 << 12);
257257
assert_eq!(
258258
one.sample::<$ty, _>(Open01),
259259
$EPSILON / two * $ty::splat(3.0)
260260
);
261-
let mut max = StepRng::new(!0, 0);
261+
let mut max = const_rng(!0);
262262
assert_eq!(
263263
max.sample::<$ty, _>(Open01),
264264
$ty::splat(1.0) - $EPSILON / two

src/distr/uniform_float.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,14 @@ uniform_float_impl! { feature = "simd_support", f64x8, u64x8, f64, u64, 64 - 52
219219
mod tests {
220220
use super::*;
221221
use crate::distr::{utils::FloatSIMDScalarUtils, Uniform};
222-
use crate::rngs::mock::StepRng;
222+
use crate::test::{const_rng, step_rng};
223223

224224
#[test]
225225
#[cfg_attr(miri, ignore)] // Miri is too slow
226226
fn test_floats() {
227227
let mut rng = crate::test::rng(252);
228-
let mut zero_rng = StepRng::new(0, 0);
229-
let mut max_rng = StepRng::new(0xffff_ffff_ffff_ffff, 0);
228+
let mut zero_rng = const_rng(0);
229+
let mut max_rng = const_rng(0xffff_ffff_ffff_ffff);
230230
macro_rules! t {
231231
($ty:ty, $f_scalar:ident, $bits_shifted:expr) => {{
232232
let v: &[($f_scalar, $f_scalar)] = &[
@@ -318,10 +318,8 @@ mod tests {
318318
// since for those rounding might result in selecting high for a very
319319
// long time.
320320
if (high_scalar - low_scalar) > 0.0001 {
321-
let mut lowering_max_rng = StepRng::new(
322-
0xffff_ffff_ffff_ffff,
323-
(-1i64 << $bits_shifted) as u64,
324-
);
321+
let mut lowering_max_rng =
322+
step_rng(0xffff_ffff_ffff_ffff, (-1i64 << $bits_shifted) as u64);
325323
assert!(
326324
<$ty as SampleUniform>::Sampler::sample_single(
327325
low,

src/lib.rs

+28
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,34 @@ mod test {
309309
rand_pcg::Pcg32::new(seed, INC)
310310
}
311311

312+
/// Construct a generator yielding a constant value
313+
pub fn const_rng(x: u64) -> StepRng {
314+
StepRng(x, 0)
315+
}
316+
317+
/// Construct a generator yielding an arithmetic sequence
318+
pub fn step_rng(x: u64, increment: u64) -> StepRng {
319+
StepRng(x, increment)
320+
}
321+
322+
#[derive(Clone)]
323+
pub struct StepRng(u64, u64);
324+
impl RngCore for StepRng {
325+
fn next_u32(&mut self) -> u32 {
326+
self.next_u64() as u32
327+
}
328+
329+
fn next_u64(&mut self) -> u64 {
330+
let res = self.0;
331+
self.0 = self.0.wrapping_add(self.1);
332+
res
333+
}
334+
335+
fn fill_bytes(&mut self, dst: &mut [u8]) {
336+
rand_core::impls::fill_bytes_via_next(self, dst)
337+
}
338+
}
339+
312340
#[test]
313341
#[cfg(feature = "thread_rng")]
314342
fn test_random() {

src/rng.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ pub trait Rng: RngCore {
110110
/// # Example
111111
///
112112
/// ```
113-
/// use rand::{rngs::mock::StepRng, Rng};
113+
/// use rand::{rngs::SmallRng, Rng, SeedableRng};
114114
///
115-
/// let rng = StepRng::new(1, 1);
115+
/// let rng = SmallRng::seed_from_u64(0);
116116
/// let v: Vec<i32> = rng.random_iter().take(5).collect();
117-
/// assert_eq!(&v, &[1, 2, 3, 4, 5]);
117+
/// assert_eq!(v.len(), 5);
118118
/// ```
119119
#[inline]
120120
fn random_iter<T>(self) -> distr::Iter<StandardUniform, Self, T>
@@ -479,14 +479,13 @@ where
479479
#[cfg(test)]
480480
mod test {
481481
use super::*;
482-
use crate::rngs::mock::StepRng;
483-
use crate::test::rng;
482+
use crate::test::{const_rng, rng};
484483
#[cfg(feature = "alloc")]
485484
use alloc::boxed::Box;
486485

487486
#[test]
488487
fn test_fill_bytes_default() {
489-
let mut r = StepRng::new(0x11_22_33_44_55_66_77_88, 0);
488+
let mut r = const_rng(0x11_22_33_44_55_66_77_88);
490489

491490
// check every remainder mod 8, both in small and big vectors.
492491
let lengths = [0, 1, 2, 3, 4, 5, 6, 7, 80, 81, 82, 83, 84, 85, 86, 87];
@@ -507,7 +506,7 @@ mod test {
507506
#[test]
508507
fn test_fill() {
509508
let x = 9041086907909331047; // a random u64
510-
let mut rng = StepRng::new(x, 0);
509+
let mut rng = const_rng(x);
511510

512511
// Convert to byte sequence and back to u64; byte-swap twice if BE.
513512
let mut array = [0u64; 2];
@@ -537,7 +536,7 @@ mod test {
537536
#[test]
538537
fn test_fill_empty() {
539538
let mut array = [0u32; 0];
540-
let mut rng = StepRng::new(0, 1);
539+
let mut rng = rng(1);
541540
rng.fill(&mut array);
542541
rng.fill(&mut array[..]);
543542
}

src/rngs/mock.rs

+4-27
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
//! Mock random number generator
1010
11+
#![allow(deprecated)]
12+
1113
use rand_core::{impls, RngCore};
1214

1315
#[cfg(feature = "serde")]
@@ -31,6 +33,7 @@ use serde::{Deserialize, Serialize};
3133
/// # Example
3234
///
3335
/// ```
36+
/// # #![allow(deprecated)]
3437
/// use rand::Rng;
3538
/// use rand::rngs::mock::StepRng;
3639
///
@@ -40,6 +43,7 @@ use serde::{Deserialize, Serialize};
4043
/// ```
4144
#[derive(Debug, Clone, PartialEq, Eq)]
4245
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
46+
#[deprecated(since = "0.9.2", note = "Deprecated without replacement")]
4347
pub struct StepRng {
4448
v: u64,
4549
a: u64,
@@ -74,30 +78,3 @@ impl RngCore for StepRng {
7478
impls::fill_bytes_via_next(self, dst)
7579
}
7680
}
77-
78-
#[cfg(test)]
79-
mod tests {
80-
#[cfg(any(feature = "alloc", feature = "serde"))]
81-
use super::StepRng;
82-
83-
#[test]
84-
#[cfg(feature = "serde")]
85-
fn test_serialization_step_rng() {
86-
let some_rng = StepRng::new(42, 7);
87-
let de_some_rng: StepRng =
88-
bincode::deserialize(&bincode::serialize(&some_rng).unwrap()).unwrap();
89-
assert_eq!(some_rng.v, de_some_rng.v);
90-
assert_eq!(some_rng.a, de_some_rng.a);
91-
}
92-
93-
#[test]
94-
#[cfg(feature = "alloc")]
95-
fn test_bool() {
96-
use crate::{distr::StandardUniform, Rng};
97-
98-
// If this result ever changes, update doc on StepRng!
99-
let rng = StepRng::new(0, 1 << 31);
100-
let result: alloc::vec::Vec<bool> = rng.sample_iter(StandardUniform).take(6).collect();
101-
assert_eq!(&result, &[false, true, false, true, false, true]);
102-
}
103-
}

src/rngs/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
mod reseeding;
8181
pub use reseeding::ReseedingRng;
8282

83+
#[deprecated(since = "0.9.2")]
8384
pub mod mock; // Public so we don't export `StepRng` directly, making it a bit
8485
// more clear it is intended for testing.
8586

src/rngs/reseeding.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,15 @@ where
253253
#[cfg(feature = "std_rng")]
254254
#[cfg(test)]
255255
mod test {
256-
use crate::rngs::mock::StepRng;
257256
use crate::rngs::std::Core;
257+
use crate::test::const_rng;
258258
use crate::Rng;
259259

260260
use super::ReseedingRng;
261261

262262
#[test]
263263
fn test_reseeding() {
264-
let zero = StepRng::new(0, 0);
264+
let zero = const_rng(0);
265265
let thresh = 1; // reseed every time the buffer is exhausted
266266
let mut reseeding = ReseedingRng::<Core, _>::new(thresh, zero).unwrap();
267267

@@ -281,7 +281,7 @@ mod test {
281281
#[test]
282282
#[allow(clippy::redundant_clone)]
283283
fn test_clone_reseeding() {
284-
let zero = StepRng::new(0, 0);
284+
let zero = const_rng(0);
285285
let mut rng1 = ReseedingRng::<Core, _>::new(32 * 4, zero).unwrap();
286286

287287
let first: u32 = rng1.random();

0 commit comments

Comments
 (0)