Skip to content

Commit c68463c

Browse files
authored
Remove SmallRng::from_thread_rng (#1532)
1 parent 4807e26 commit c68463c

File tree

9 files changed

+77
-78
lines changed

9 files changed

+77
-78
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
3232
- Rename `Rng::gen_range` to `random_range`, `gen_bool` to `random_bool`, `gen_ratio` to `random_ratio` (#1505)
3333
- Rename `Standard` to `StandardUniform` (#1526)
3434
- Remove impl of `Distribution<Option<T>>` for `Standard` (#1526)
35+
- Remove `SmallRng::from_thread_rng` (#1532)
3536
- Remove first parameter (`rng`) of `ReseedingRng::new` (#1533)
3637

3738
## [0.9.0-alpha.1] - 2024-03-18

benches/benches/distr.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const ITER_ELTS: u64 = 100;
2525
macro_rules! distr_int {
2626
($group:ident, $fnn:expr, $ty:ty, $distr:expr) => {
2727
$group.bench_function($fnn, |c| {
28-
let mut rng = Pcg64Mcg::from_os_rng();
28+
let mut rng = Pcg64Mcg::from_rng(&mut rand::rng());
2929
let distr = $distr;
3030

3131
c.iter(|| distr.sample(&mut rng));
@@ -36,7 +36,7 @@ macro_rules! distr_int {
3636
macro_rules! distr_float {
3737
($group:ident, $fnn:expr, $ty:ty, $distr:expr) => {
3838
$group.bench_function($fnn, |c| {
39-
let mut rng = Pcg64Mcg::from_os_rng();
39+
let mut rng = Pcg64Mcg::from_rng(&mut rand::rng());
4040
let distr = $distr;
4141

4242
c.iter(|| Distribution::<$ty>::sample(&distr, &mut rng));
@@ -47,7 +47,7 @@ macro_rules! distr_float {
4747
macro_rules! distr_arr {
4848
($group:ident, $fnn:expr, $ty:ty, $distr:expr) => {
4949
$group.bench_function($fnn, |c| {
50-
let mut rng = Pcg64Mcg::from_os_rng();
50+
let mut rng = Pcg64Mcg::from_rng(&mut rand::rng());
5151
let distr = $distr;
5252

5353
c.iter(|| Distribution::<$ty>::sample(&distr, &mut rng));
@@ -76,7 +76,7 @@ fn bench(c: &mut Criterion<CyclesPerByte>) {
7676
g.throughput(Throughput::Elements(ITER_ELTS));
7777
g.bench_function("iter", |c| {
7878
use core::f64::consts::{E, PI};
79-
let mut rng = Pcg64Mcg::from_os_rng();
79+
let mut rng = Pcg64Mcg::from_rng(&mut rand::rng());
8080
let distr = Normal::new(-E, PI).unwrap();
8181

8282
c.iter(|| {
@@ -145,7 +145,7 @@ fn bench(c: &mut Criterion<CyclesPerByte>) {
145145
}
146146
g.throughput(Throughput::Elements(ITER_ELTS));
147147
g.bench_function("variable", |c| {
148-
let mut rng = Pcg64Mcg::from_os_rng();
148+
let mut rng = Pcg64Mcg::from_rng(&mut rand::rng());
149149
let ldistr = Uniform::new(0.1, 10.0).unwrap();
150150

151151
c.iter(|| {
@@ -165,7 +165,7 @@ fn bench(c: &mut Criterion<CyclesPerByte>) {
165165

166166
let mut g = c.benchmark_group("bernoulli");
167167
g.bench_function("bernoulli", |c| {
168-
let mut rng = Pcg64Mcg::from_os_rng();
168+
let mut rng = Pcg64Mcg::from_rng(&mut rand::rng());
169169
let distr = Bernoulli::new(0.18).unwrap();
170170
c.iter(|| distr.sample(&mut rng))
171171
});

benches/benches/generators.rs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ pub fn random_bytes(c: &mut Criterion) {
4040
}
4141

4242
bench(&mut g, "step", StepRng::new(0, 1));
43-
bench(&mut g, "pcg32", Pcg32::from_os_rng());
44-
bench(&mut g, "pcg64", Pcg64::from_os_rng());
45-
bench(&mut g, "pcg64mcg", Pcg64Mcg::from_os_rng());
46-
bench(&mut g, "pcg64dxsm", Pcg64Dxsm::from_os_rng());
47-
bench(&mut g, "chacha8", ChaCha8Rng::from_os_rng());
48-
bench(&mut g, "chacha12", ChaCha12Rng::from_os_rng());
49-
bench(&mut g, "chacha20", ChaCha20Rng::from_os_rng());
50-
bench(&mut g, "std", StdRng::from_os_rng());
51-
bench(&mut g, "small", SmallRng::from_thread_rng());
43+
bench(&mut g, "pcg32", Pcg32::from_rng(&mut rand::rng()));
44+
bench(&mut g, "pcg64", Pcg64::from_rng(&mut rand::rng()));
45+
bench(&mut g, "pcg64mcg", Pcg64Mcg::from_rng(&mut rand::rng()));
46+
bench(&mut g, "pcg64dxsm", Pcg64Dxsm::from_rng(&mut rand::rng()));
47+
bench(&mut g, "chacha8", ChaCha8Rng::from_rng(&mut rand::rng()));
48+
bench(&mut g, "chacha12", ChaCha12Rng::from_rng(&mut rand::rng()));
49+
bench(&mut g, "chacha20", ChaCha20Rng::from_rng(&mut rand::rng()));
50+
bench(&mut g, "std", StdRng::from_rng(&mut rand::rng()));
51+
bench(&mut g, "small", SmallRng::from_rng(&mut rand::rng()));
5252
bench(&mut g, "os", UnwrapErr(OsRng));
5353
bench(&mut g, "thread", rand::rng());
5454

@@ -69,15 +69,15 @@ pub fn random_u32(c: &mut Criterion) {
6969
}
7070

7171
bench(&mut g, "step", StepRng::new(0, 1));
72-
bench(&mut g, "pcg32", Pcg32::from_os_rng());
73-
bench(&mut g, "pcg64", Pcg64::from_os_rng());
74-
bench(&mut g, "pcg64mcg", Pcg64Mcg::from_os_rng());
75-
bench(&mut g, "pcg64dxsm", Pcg64Dxsm::from_os_rng());
76-
bench(&mut g, "chacha8", ChaCha8Rng::from_os_rng());
77-
bench(&mut g, "chacha12", ChaCha12Rng::from_os_rng());
78-
bench(&mut g, "chacha20", ChaCha20Rng::from_os_rng());
79-
bench(&mut g, "std", StdRng::from_os_rng());
80-
bench(&mut g, "small", SmallRng::from_thread_rng());
72+
bench(&mut g, "pcg32", Pcg32::from_rng(&mut rand::rng()));
73+
bench(&mut g, "pcg64", Pcg64::from_rng(&mut rand::rng()));
74+
bench(&mut g, "pcg64mcg", Pcg64Mcg::from_rng(&mut rand::rng()));
75+
bench(&mut g, "pcg64dxsm", Pcg64Dxsm::from_rng(&mut rand::rng()));
76+
bench(&mut g, "chacha8", ChaCha8Rng::from_rng(&mut rand::rng()));
77+
bench(&mut g, "chacha12", ChaCha12Rng::from_rng(&mut rand::rng()));
78+
bench(&mut g, "chacha20", ChaCha20Rng::from_rng(&mut rand::rng()));
79+
bench(&mut g, "std", StdRng::from_rng(&mut rand::rng()));
80+
bench(&mut g, "small", SmallRng::from_rng(&mut rand::rng()));
8181
bench(&mut g, "os", UnwrapErr(OsRng));
8282
bench(&mut g, "thread", rand::rng());
8383

@@ -98,15 +98,15 @@ pub fn random_u64(c: &mut Criterion) {
9898
}
9999

100100
bench(&mut g, "step", StepRng::new(0, 1));
101-
bench(&mut g, "pcg32", Pcg32::from_os_rng());
102-
bench(&mut g, "pcg64", Pcg64::from_os_rng());
103-
bench(&mut g, "pcg64mcg", Pcg64Mcg::from_os_rng());
104-
bench(&mut g, "pcg64dxsm", Pcg64Dxsm::from_os_rng());
105-
bench(&mut g, "chacha8", ChaCha8Rng::from_os_rng());
106-
bench(&mut g, "chacha12", ChaCha12Rng::from_os_rng());
107-
bench(&mut g, "chacha20", ChaCha20Rng::from_os_rng());
108-
bench(&mut g, "std", StdRng::from_os_rng());
109-
bench(&mut g, "small", SmallRng::from_thread_rng());
101+
bench(&mut g, "pcg32", Pcg32::from_rng(&mut rand::rng()));
102+
bench(&mut g, "pcg64", Pcg64::from_rng(&mut rand::rng()));
103+
bench(&mut g, "pcg64mcg", Pcg64Mcg::from_rng(&mut rand::rng()));
104+
bench(&mut g, "pcg64dxsm", Pcg64Dxsm::from_rng(&mut rand::rng()));
105+
bench(&mut g, "chacha8", ChaCha8Rng::from_rng(&mut rand::rng()));
106+
bench(&mut g, "chacha12", ChaCha12Rng::from_rng(&mut rand::rng()));
107+
bench(&mut g, "chacha20", ChaCha20Rng::from_rng(&mut rand::rng()));
108+
bench(&mut g, "std", StdRng::from_rng(&mut rand::rng()));
109+
bench(&mut g, "small", SmallRng::from_rng(&mut rand::rng()));
110110
bench(&mut g, "os", UnwrapErr(OsRng));
111111
bench(&mut g, "thread", rand::rng());
112112

@@ -120,7 +120,7 @@ pub fn init_gen(c: &mut Criterion) {
120120

121121
fn bench<R: SeedableRng>(g: &mut BenchmarkGroup<WallTime>, name: &str) {
122122
g.bench_function(name, |b| {
123-
let mut rng = Pcg32::from_os_rng();
123+
let mut rng = Pcg32::from_rng(&mut rand::rng());
124124
b.iter(|| R::from_rng(&mut rng));
125125
});
126126
}
@@ -145,7 +145,7 @@ pub fn init_from_u64(c: &mut Criterion) {
145145

146146
fn bench<R: SeedableRng>(g: &mut BenchmarkGroup<WallTime>, name: &str) {
147147
g.bench_function(name, |b| {
148-
let mut rng = Pcg32::from_os_rng();
148+
let mut rng = Pcg32::from_rng(&mut rand::rng());
149149
let seed = rng.random();
150150
b.iter(|| R::seed_from_u64(black_box(seed)));
151151
});
@@ -174,7 +174,7 @@ pub fn init_from_seed(c: &mut Criterion) {
174174
rand::distr::StandardUniform: Distribution<<R as SeedableRng>::Seed>,
175175
{
176176
g.bench_function(name, |b| {
177-
let mut rng = Pcg32::from_os_rng();
177+
let mut rng = Pcg32::from_rng(&mut rand::rng());
178178
let seed = rng.random();
179179
b.iter(|| R::from_seed(black_box(seed.clone())));
180180
});

benches/benches/standard.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ where
2727
{
2828
g.throughput(criterion::Throughput::Bytes(size_of::<T>() as u64));
2929
g.bench_function(name, |b| {
30-
let mut rng = Pcg64Mcg::from_os_rng();
30+
let mut rng = Pcg64Mcg::from_rng(&mut rand::rng());
3131

3232
b.iter(|| rng.sample::<T, _>(D::default()));
3333
});

rand_chacha/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
//! or a deterministic generator such as [`ChaCha20Rng`].
4747
//! Beware that should a weak master generator be used, correlations may be
4848
//! detectable between the outputs of its child generators.
49+
//! ```ignore
50+
//! let rng = ChaCha12Rng::from_rng(&mut rand::rng());
51+
//! ```
4952
//!
5053
//! See also [Seeding RNGs] in the book.
5154
//!

rand_pcg/src/lib.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,21 @@
3434
//! Generators implement the [`SeedableRng`] trait. All methods are suitable for
3535
//! seeding. Some suggestions:
3636
//!
37-
//! 1. Seed **from an integer** via `seed_from_u64`. This uses a hash function
38-
//! internally to yield a (typically) good seed from any input.
39-
//! ```
40-
//! # use {rand_core::SeedableRng, rand_pcg::Pcg64Mcg};
41-
//! let rng = Pcg64Mcg::seed_from_u64(1);
37+
//! 1. To automatically seed with a unique seed, use [`SeedableRng::from_rng`]
38+
//! with a master generator (here [`rand::rng()`](https://docs.rs/rand/latest/rand/fn.rng.html)):
39+
//! ```ignore
40+
//! use rand_core::SeedableRng;
41+
//! use rand_pcg::Pcg64Mcg;
42+
//! let rng = Pcg64Mcg::from_rng(&mut rand::rng());
4243
//! # let _: Pcg64Mcg = rng;
4344
//! ```
44-
//! 2. With a fresh seed, **direct from the OS** (implies a syscall):
45+
//! 2. Seed **from an integer** via `seed_from_u64`. This uses a hash function
46+
//! internally to yield a (typically) good seed from any input.
4547
//! ```
4648
//! # use {rand_core::SeedableRng, rand_pcg::Pcg64Mcg};
47-
//! let rng = Pcg64Mcg::from_os_rng();
49+
//! let rng = Pcg64Mcg::seed_from_u64(1);
4850
//! # let _: Pcg64Mcg = rng;
4951
//! ```
50-
//! 3. **From a master generator.** This could be [`rand::rng`]
51-
//! (effectively a fresh seed without the need for a syscall on each usage)
52-
//! or a deterministic generator such as [`rand_chacha::ChaCha8Rng`].
53-
//! Beware that should a weak master generator be used, correlations may be
54-
//! detectable between the outputs of its child generators.
5552
//!
5653
//! See also [Seeding RNGs] in the book.
5754
//!
@@ -77,6 +74,7 @@
7774
//! [Random Values]: https://rust-random.github.io/book/guide-values.html
7875
//! [`RngCore`]: rand_core::RngCore
7976
//! [`SeedableRng`]: rand_core::SeedableRng
77+
//! [`SeedableRng::from_rng`]: rand_core::SeedableRng#method.from_rng
8078
//! [`rand::rng`]: https://docs.rs/rand/latest/rand/fn.rng.html
8179
//! [`rand::Rng`]: https://docs.rs/rand/latest/rand/trait.Rng.html
8280
//! [`rand_chacha::ChaCha8Rng`]: https://docs.rs/rand_chacha/latest/rand_chacha/struct.ChaCha8Rng.html

src/distr/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ use crate::Rng;
182182
/// use rand::prelude::*;
183183
/// use rand::distr::StandardUniform;
184184
///
185-
/// let val: f32 = StdRng::from_os_rng().sample(StandardUniform);
185+
/// let val: f32 = rand::rng().sample(StandardUniform);
186186
/// println!("f32 from [0, 1): {}", val);
187187
/// ```
188188
///

src/rngs/small.rs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,29 @@ type Rng = super::xoshiro256plusplus::Xoshiro256PlusPlus;
4040
/// suitable for seeding, but note that, even with a fixed seed, output is not
4141
/// [portable]. Some suggestions:
4242
///
43-
/// 1. Seed **from an integer** via `seed_from_u64`. This uses a hash function
44-
/// internally to yield a (typically) good seed from any input.
43+
/// 1. To automatically seed with a unique seed, use [`SeedableRng::from_rng`]:
4544
/// ```
46-
/// # use rand::{SeedableRng, rngs::SmallRng};
47-
/// let rng = SmallRng::seed_from_u64(1);
45+
/// use rand::SeedableRng;
46+
/// use rand::rngs::SmallRng;
47+
/// let rng = SmallRng::from_rng(&mut rand::rng());
4848
/// # let _: SmallRng = rng;
4949
/// ```
50-
/// 2. With a fresh seed, **direct from the OS** (implies a syscall):
50+
/// or [`SeedableRng::from_os_rng`]:
5151
/// ```
52-
/// # use rand::{SeedableRng, rngs::SmallRng};
52+
/// # use rand::SeedableRng;
53+
/// # use rand::rngs::SmallRng;
5354
/// let rng = SmallRng::from_os_rng();
5455
/// # let _: SmallRng = rng;
5556
/// ```
56-
/// 3. Via [`SmallRng::from_thread_rng`]:
57+
/// 2. To use a deterministic integral seed, use `seed_from_u64`. This uses a
58+
/// hash function internally to yield a (typically) good seed from any
59+
/// input.
5760
/// ```
58-
/// # use rand::rngs::SmallRng;
59-
/// let rng = SmallRng::from_thread_rng();
61+
/// # use rand::{SeedableRng, rngs::SmallRng};
62+
/// let rng = SmallRng::seed_from_u64(1);
63+
/// # let _: SmallRng = rng;
6064
/// ```
65+
/// 3. To seed deterministically from text or other input, use [`rand_seeder`].
6166
///
6267
/// See also [Seeding RNGs] in the book.
6368
///
@@ -74,6 +79,7 @@ type Rng = super::xoshiro256plusplus::Xoshiro256PlusPlus;
7479
/// [rand_pcg]: https://crates.io/crates/rand_pcg
7580
/// [rand_xoshiro]: https://crates.io/crates/rand_xoshiro
7681
/// [`rand_chacha::ChaCha8Rng`]: https://docs.rs/rand_chacha/latest/rand_chacha/struct.ChaCha8Rng.html
82+
/// [`rand_seeder`]: https://docs.rs/rand_seeder/latest/rand_seeder/
7783
#[derive(Clone, Debug, PartialEq, Eq)]
7884
pub struct SmallRng(Rng);
7985

@@ -112,19 +118,3 @@ impl RngCore for SmallRng {
112118
self.0.fill_bytes(dest)
113119
}
114120
}
115-
116-
impl SmallRng {
117-
/// Construct an instance seeded from `rand::rng`
118-
///
119-
/// # Panics
120-
///
121-
/// This method panics only if [`crate::rng()`] fails to
122-
/// initialize.
123-
#[cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))]
124-
#[inline(always)]
125-
pub fn from_thread_rng() -> Self {
126-
let mut seed = <Rng as SeedableRng>::Seed::default();
127-
crate::rng().fill_bytes(seed.as_mut());
128-
SmallRng(Rng::from_seed(seed))
129-
}
130-
}

src/rngs/std.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,22 @@ use rand_chacha::ChaCha12Rng as Rng;
3737
/// but note that `seed_from_u64` is not suitable for usage where security is
3838
/// important. Also note that, even with a fixed seed, output is not [portable].
3939
///
40-
/// It is suggested to use a fresh seed **direct from the OS** as the most
41-
/// secure and convenient option:
40+
/// Using a fresh seed **direct from the OS** is the most secure option:
4241
/// ```
4342
/// # use rand::{SeedableRng, rngs::StdRng};
4443
/// let rng = StdRng::from_os_rng();
4544
/// # let _: StdRng = rng;
4645
/// ```
4746
///
48-
/// See also [Seeding RNGs] in the book.
47+
/// Seeding via [`rand::rng()`](crate::rng()) may be faster:
48+
/// ```
49+
/// # use rand::{SeedableRng, rngs::StdRng};
50+
/// let rng = StdRng::from_rng(&mut rand::rng());
51+
/// # let _: StdRng = rng;
52+
/// ```
53+
///
54+
/// Any [`SeedableRng`] method may be used, but note that `seed_from_u64` is not
55+
/// suitable where security is required. See also [Seeding RNGs] in the book.
4956
///
5057
/// ## Generation
5158
///

0 commit comments

Comments
 (0)