Skip to content

Commit f5185d9

Browse files
authored
thread_rng() → rand::rng() (#1506)
- Rename `rand::thread_rng()` → `rand::rng()` - Remove `thread_rng()` and `random()` from the prelude
1 parent 0fba940 commit f5185d9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+174
-167
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
2828
- Implement `Distribution<u64>` for `Poisson<f64>` (#1498)
2929
- Limit the maximal acceptable lambda for `Poisson` to solve (#1312) (#1498)
3030
- Rename `Rng::gen_iter` to `random_iter` (#1500)
31+
- Rename `rand::thread_rng()` to `rand::rng()`, and remove from the prelude (#1506)
32+
- Remove `rand::random()` from the prelude (#1506)
3133

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

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Rand is a Rust library supporting random generators:
1111
- A standard RNG trait: [`rand_core::RngCore`](https://docs.rs/rand_core/latest/rand_core/trait.RngCore.html)
1212
- Fast implementations of the best-in-class [cryptographic](https://rust-random.github.io/book/guide-rngs.html#cryptographically-secure-pseudo-random-number-generators-csprngs) and
1313
[non-cryptographic](https://rust-random.github.io/book/guide-rngs.html#basic-pseudo-random-number-generators-prngs) generators: [`rand::rngs`](https://docs.rs/rand/latest/rand/rngs/index.html), and more RNGs: [`rand_chacha`](https://docs.rs/rand_chacha), [`rand_xoshiro`](https://docs.rs/rand_xoshiro/), [`rand_pcg`](https://docs.rs/rand_pcg/), [rngs repo](https://github.com/rust-random/rngs/)
14-
- [`rand::thread_rng`](https://docs.rs/rand/latest/rand/fn.thread_rng.html) is an asymtotically-fast, reasonably secure generator available on all `std` targets
14+
- [`rand::rng`](https://docs.rs/rand/latest/rand/fn.rng.html) is an asymtotically-fast, reasonably secure generator available on all `std` targets
1515
- Secure seeding via the [`getrandom` crate](https://crates.io/crates/getrandom)
1616

1717
Supporting random value generation and random processes:
@@ -78,7 +78,7 @@ Rand is built with these features enabled by default:
7878
- `alloc` (implied by `std`) enables functionality requiring an allocator
7979
- `getrandom` (implied by `std`) is an optional dependency providing the code
8080
behind `rngs::OsRng`
81-
- `std_rng` enables inclusion of `StdRng`, `thread_rng`
81+
- `std_rng` enables inclusion of `StdRng`, `ThreadRng`
8282

8383
Optionally, the following dependencies can be enabled:
8484

@@ -98,7 +98,7 @@ experimental `simd_support` feature.
9898
Rand supports limited functionality in `no_std` mode (enabled via
9999
`default-features = false`). In this case, `OsRng` and `from_os_rng` are
100100
unavailable (unless `getrandom` is enabled), large parts of `seq` are
101-
unavailable (unless `alloc` is enabled), and `thread_rng` is unavailable.
101+
unavailable (unless `alloc` is enabled), and `ThreadRng` is unavailable.
102102

103103
## Portability and platform support
104104

SECURITY.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ Explanation of exceptions:
5252
- Jitter: `JitterRng` is used as an entropy source when the primary source
5353
fails; this source may not be secure against side-channel attacks, see #699.
5454
- ISAAC: the [ISAAC](https://burtleburtle.net/bob/rand/isaacafa.html) RNG used
55-
to implement `thread_rng` is difficult to analyse and thus cannot provide
55+
to implement `ThreadRng` is difficult to analyse and thus cannot provide
5656
strong assertions of security.
5757

5858
## Known issues
5959

60-
In `rand` version 0.3 (0.3.18 and later), if `OsRng` fails, `thread_rng` is
60+
In `rand` version 0.3 (0.3.18 and later), if `OsRng` fails, `ThreadRng` is
6161
seeded from the system time in an insecure manner.
6262

6363
## Reporting a Vulnerability

benches/benches/array.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,31 @@ pub fn bench(c: &mut Criterion) {
2626

2727
g.bench_function("u16_iter_repeat", |b| {
2828
use core::iter;
29-
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
29+
let mut rng = Pcg64Mcg::from_rng(&mut rand::rng());
3030
b.iter(|| {
3131
let v: Vec<u16> = iter::repeat(()).map(|()| rng.random()).take(512).collect();
3232
v
3333
});
3434
});
3535

3636
g.bench_function("u16_sample_iter", |b| {
37-
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
37+
let mut rng = Pcg64Mcg::from_rng(&mut rand::rng());
3838
b.iter(|| {
3939
let v: Vec<u16> = Standard.sample_iter(&mut rng).take(512).collect();
4040
v
4141
});
4242
});
4343

4444
g.bench_function("u16_gen_array", |b| {
45-
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
45+
let mut rng = Pcg64Mcg::from_rng(&mut rand::rng());
4646
b.iter(|| {
4747
let v: [u16; 512] = rng.random();
4848
v
4949
});
5050
});
5151

5252
g.bench_function("u16_fill", |b| {
53-
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
53+
let mut rng = Pcg64Mcg::from_rng(&mut rand::rng());
5454
let mut buf = [0u16; 512];
5555
b.iter(|| {
5656
rng.fill(&mut buf[..]);
@@ -60,31 +60,31 @@ pub fn bench(c: &mut Criterion) {
6060

6161
g.bench_function("u64_iter_repeat", |b| {
6262
use core::iter;
63-
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
63+
let mut rng = Pcg64Mcg::from_rng(&mut rand::rng());
6464
b.iter(|| {
6565
let v: Vec<u64> = iter::repeat(()).map(|()| rng.random()).take(128).collect();
6666
v
6767
});
6868
});
6969

7070
g.bench_function("u64_sample_iter", |b| {
71-
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
71+
let mut rng = Pcg64Mcg::from_rng(&mut rand::rng());
7272
b.iter(|| {
7373
let v: Vec<u64> = Standard.sample_iter(&mut rng).take(128).collect();
7474
v
7575
});
7676
});
7777

7878
g.bench_function("u64_gen_array", |b| {
79-
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
79+
let mut rng = Pcg64Mcg::from_rng(&mut rand::rng());
8080
b.iter(|| {
8181
let v: [u64; 128] = rng.random();
8282
v
8383
});
8484
});
8585

8686
g.bench_function("u64_fill", |b| {
87-
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
87+
let mut rng = Pcg64Mcg::from_rng(&mut rand::rng());
8888
let mut buf = [0u64; 128];
8989
b.iter(|| {
9090
rng.fill(&mut buf[..]);

benches/benches/bool.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,41 @@ pub fn bench(c: &mut Criterion) {
2727
g.measurement_time(core::time::Duration::from_millis(1000));
2828

2929
g.bench_function("standard", |b| {
30-
let mut rng = Pcg32::from_rng(&mut thread_rng());
30+
let mut rng = Pcg32::from_rng(&mut rand::rng());
3131
b.iter(|| rng.sample::<bool, _>(rand::distr::Standard))
3232
});
3333

3434
g.bench_function("const", |b| {
35-
let mut rng = Pcg32::from_rng(&mut thread_rng());
35+
let mut rng = Pcg32::from_rng(&mut rand::rng());
3636
b.iter(|| rng.gen_bool(0.18))
3737
});
3838

3939
g.bench_function("var", |b| {
40-
let mut rng = Pcg32::from_rng(&mut thread_rng());
40+
let mut rng = Pcg32::from_rng(&mut rand::rng());
4141
let p = rng.random();
4242
b.iter(|| rng.gen_bool(p))
4343
});
4444

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

5050
g.bench_function("ratio_var", |b| {
51-
let mut rng = Pcg32::from_rng(&mut thread_rng());
51+
let mut rng = Pcg32::from_rng(&mut rand::rng());
5252
let d = rng.gen_range(1..=100);
5353
let n = rng.gen_range(0..=d);
5454
b.iter(|| rng.gen_ratio(n, d));
5555
});
5656

5757
g.bench_function("bernoulli_const", |b| {
58-
let mut rng = Pcg32::from_rng(&mut thread_rng());
58+
let mut rng = Pcg32::from_rng(&mut rand::rng());
5959
let d = Bernoulli::new(0.18).unwrap();
6060
b.iter(|| rng.sample(d))
6161
});
6262

6363
g.bench_function("bernoulli_var", |b| {
64-
let mut rng = Pcg32::from_rng(&mut thread_rng());
64+
let mut rng = Pcg32::from_rng(&mut rand::rng());
6565
let p = rng.random();
6666
let d = Bernoulli::new(p).unwrap();
6767
b.iter(|| rng.sample(d))

benches/benches/generators.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub fn gen_bytes(c: &mut Criterion) {
5050
bench(&mut g, "std", StdRng::from_os_rng());
5151
bench(&mut g, "small", SmallRng::from_thread_rng());
5252
bench(&mut g, "os", UnwrapErr(OsRng));
53-
bench(&mut g, "thread", thread_rng());
53+
bench(&mut g, "thread", rand::rng());
5454

5555
g.finish()
5656
}
@@ -79,7 +79,7 @@ pub fn gen_u32(c: &mut Criterion) {
7979
bench(&mut g, "std", StdRng::from_os_rng());
8080
bench(&mut g, "small", SmallRng::from_thread_rng());
8181
bench(&mut g, "os", UnwrapErr(OsRng));
82-
bench(&mut g, "thread", thread_rng());
82+
bench(&mut g, "thread", rand::rng());
8383

8484
g.finish()
8585
}
@@ -108,7 +108,7 @@ pub fn gen_u64(c: &mut Criterion) {
108108
bench(&mut g, "std", StdRng::from_os_rng());
109109
bench(&mut g, "small", SmallRng::from_thread_rng());
110110
bench(&mut g, "os", UnwrapErr(OsRng));
111-
bench(&mut g, "thread", thread_rng());
111+
bench(&mut g, "thread", rand::rng());
112112

113113
g.finish()
114114
}

benches/benches/seq_choose.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ criterion_main!(benches);
2020

2121
pub fn bench(c: &mut Criterion) {
2222
c.bench_function("seq_slice_choose_1_of_100", |b| {
23-
let mut rng = Pcg32::from_rng(&mut thread_rng());
23+
let mut rng = Pcg32::from_rng(&mut rand::rng());
2424
let mut buf = [0i32; 100];
2525
rng.fill(&mut buf);
2626
let x = black_box(&mut buf);
@@ -32,7 +32,7 @@ pub fn bench(c: &mut Criterion) {
3232
for (amount, len) in lens {
3333
let name = format!("seq_slice_choose_multiple_{}_of_{}", amount, len);
3434
c.bench_function(name.as_str(), |b| {
35-
let mut rng = Pcg32::from_rng(&mut thread_rng());
35+
let mut rng = Pcg32::from_rng(&mut rand::rng());
3636
let mut buf = [0i32; 1000];
3737
rng.fill(&mut buf);
3838
let x = black_box(&buf[..len]);
@@ -53,15 +53,15 @@ pub fn bench(c: &mut Criterion) {
5353
}
5454

5555
c.bench_function("seq_iter_choose_multiple_10_of_100", |b| {
56-
let mut rng = Pcg32::from_rng(&mut thread_rng());
56+
let mut rng = Pcg32::from_rng(&mut rand::rng());
5757
let mut buf = [0i32; 100];
5858
rng.fill(&mut buf);
5959
let x = black_box(&buf);
6060
b.iter(|| x.iter().cloned().choose_multiple(&mut rng, 10))
6161
});
6262

6363
c.bench_function("seq_iter_choose_multiple_fill_10_of_100", |b| {
64-
let mut rng = Pcg32::from_rng(&mut thread_rng());
64+
let mut rng = Pcg32::from_rng(&mut rand::rng());
6565
let mut buf = [0i32; 100];
6666
rng.fill(&mut buf);
6767
let x = black_box(&buf);

benches/benches/shuffle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ criterion_main!(benches);
2020

2121
pub fn bench(c: &mut Criterion) {
2222
c.bench_function("seq_shuffle_100", |b| {
23-
let mut rng = Pcg32::from_rng(&mut thread_rng());
23+
let mut rng = Pcg32::from_rng(&mut rand::rng());
2424
let mut buf = [0i32; 100];
2525
rng.fill(&mut buf);
2626
let x = black_box(&mut buf);

benches/benches/uniform.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const N_RESAMPLES: usize = 10_000;
2323
macro_rules! sample {
2424
($R:ty, $T:ty, $U:ty, $g:expr) => {
2525
$g.bench_function(BenchmarkId::new(stringify!($R), "single"), |b| {
26-
let mut rng = <$R>::from_rng(&mut thread_rng());
26+
let mut rng = <$R>::from_rng(&mut rand::rng());
2727
let x = rng.random::<$U>();
2828
let bits = (<$T>::BITS / 2);
2929
let mask = (1 as $U).wrapping_neg() >> bits;
@@ -35,7 +35,7 @@ macro_rules! sample {
3535
});
3636

3737
$g.bench_function(BenchmarkId::new(stringify!($R), "distr"), |b| {
38-
let mut rng = <$R>::from_rng(&mut thread_rng());
38+
let mut rng = <$R>::from_rng(&mut rand::rng());
3939
let x = rng.random::<$U>();
4040
let bits = (<$T>::BITS / 2);
4141
let mask = (1 as $U).wrapping_neg() >> bits;

benches/benches/uniform_float.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const N_RESAMPLES: usize = 10_000;
2727
macro_rules! single_random {
2828
($R:ty, $T:ty, $g:expr) => {
2929
$g.bench_function(BenchmarkId::new(stringify!($T), stringify!($R)), |b| {
30-
let mut rng = <$R>::from_rng(&mut thread_rng());
30+
let mut rng = <$R>::from_rng(&mut rand::rng());
3131
let (mut low, mut high);
3232
loop {
3333
low = <$T>::from_bits(rng.random());
@@ -63,7 +63,7 @@ fn single_random(c: &mut Criterion) {
6363
macro_rules! distr_random {
6464
($R:ty, $T:ty, $g:expr) => {
6565
$g.bench_function(BenchmarkId::new(stringify!($T), stringify!($R)), |b| {
66-
let mut rng = <$R>::from_rng(&mut thread_rng());
66+
let mut rng = <$R>::from_rng(&mut rand::rng());
6767
let dist = loop {
6868
let low = <$T>::from_bits(rng.random());
6969
let high = <$T>::from_bits(rng.random());

benches/benches/weighted.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ criterion_main!(benches);
2020

2121
pub fn bench(c: &mut Criterion) {
2222
c.bench_function("weighted_index_creation", |b| {
23-
let mut rng = rand::thread_rng();
23+
let mut rng = rand::rng();
2424
let weights = black_box([1u32, 2, 4, 0, 5, 1, 7, 1, 2, 3, 4, 5, 6, 7]);
2525
b.iter(|| {
2626
let distr = WeightedIndex::new(weights.to_vec()).unwrap();
@@ -29,7 +29,7 @@ pub fn bench(c: &mut Criterion) {
2929
});
3030

3131
c.bench_function("weighted_index_modification", |b| {
32-
let mut rng = rand::thread_rng();
32+
let mut rng = rand::rng();
3333
let weights = black_box([1u32, 2, 3, 0, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7]);
3434
let mut distr = WeightedIndex::new(weights.to_vec()).unwrap();
3535
b.iter(|| {
@@ -53,7 +53,7 @@ pub fn bench(c: &mut Criterion) {
5353
c.bench_function(name.as_str(), |b| {
5454
let length = black_box(length);
5555
let amount = black_box(amount);
56-
let mut rng = SmallRng::from_rng(&mut thread_rng());
56+
let mut rng = SmallRng::from_rng(&mut rand::rng());
5757
b.iter(|| sample_weighted(&mut rng, length, |idx| (1 + (idx % 100)) as u32, amount))
5858
});
5959
}

examples/monte-carlo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use rand::distr::{Distribution, Uniform};
2727

2828
fn main() {
2929
let range = Uniform::new(-1.0f64, 1.0).unwrap();
30-
let mut rng = rand::thread_rng();
30+
let mut rng = rand::rng();
3131

3232
let total = 1_000_000;
3333
let mut in_circle = 0;

examples/monty-hall.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ fn main() {
7777
// The estimation will be more accurate with more simulations
7878
let num_simulations = 10000;
7979

80-
let mut rng = rand::thread_rng();
80+
let mut rng = rand::rng();
8181
let random_door = Uniform::new(0u32, 3).unwrap();
8282

8383
let (mut switch_wins, mut switch_losses) = (0, 0);

rand_chacha/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
//! let rng = ChaCha12Rng::from_os_rng();
4242
//! # let _: ChaCha12Rng = rng;
4343
//! ```
44-
//! 2. **From a master generator.** This could be [`rand::thread_rng`]
44+
//! 2. **From a master generator.** This could be [`rand::rng`]
4545
//! (effectively a fresh seed without the need for a syscall on each usage)
4646
//! or a deterministic generator such as [`ChaCha20Rng`].
4747
//! Beware that should a weak master generator be used, correlations may be
@@ -74,7 +74,7 @@
7474
//! [`RngCore`]: rand_core::RngCore
7575
//! [`SeedableRng`]: rand_core::SeedableRng
7676
//! [`SeedableRng::from_os_rng`]: rand_core::SeedableRng::from_os_rng
77-
//! [`rand::thread_rng`]: https://docs.rs/rand/latest/rand/fn.thread_rng.html
77+
//! [`rand::rng`]: https://docs.rs/rand/latest/rand/fn.rng.html
7878
//! [`rand::Rng`]: https://docs.rs/rand/latest/rand/trait.Rng.html
7979
8080
#![doc(

rand_core/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ pub trait SeedableRng: Sized {
487487
///
488488
/// In case the overhead of using [`getrandom`] to seed *many* PRNGs is an
489489
/// issue, one may prefer to seed from a local PRNG, e.g.
490-
/// `from_rng(thread_rng()).unwrap()`.
490+
/// `from_rng(rand::rng()).unwrap()`.
491491
///
492492
/// # Panics
493493
///
@@ -508,7 +508,7 @@ pub trait SeedableRng: Sized {
508508
///
509509
/// In case the overhead of using [`getrandom`] to seed *many* PRNGs is an
510510
/// issue, one may prefer to seed from a local PRNG, e.g.
511-
/// `from_rng(&mut thread_rng()).unwrap()`.
511+
/// `from_rng(&mut rand::rng()).unwrap()`.
512512
///
513513
/// [`getrandom`]: https://docs.rs/getrandom
514514
#[cfg(feature = "getrandom")]

rand_distr/src/beta.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ struct BC<N> {
7373
/// use rand_distr::{Distribution, Beta};
7474
///
7575
/// let beta = Beta::new(2.0, 5.0).unwrap();
76-
/// let v = beta.sample(&mut rand::thread_rng());
76+
/// let v = beta.sample(&mut rand::rng());
7777
/// println!("{} is from a Beta(2, 5) distribution", v);
7878
/// ```
7979
#[derive(Clone, Copy, Debug, PartialEq)]

rand_distr/src/binomial.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use rand::Rng;
4040
/// use rand_distr::{Binomial, Distribution};
4141
///
4242
/// let bin = Binomial::new(20, 0.3).unwrap();
43-
/// let v = bin.sample(&mut rand::thread_rng());
43+
/// let v = bin.sample(&mut rand::rng());
4444
/// println!("{} is from a binomial distribution", v);
4545
/// ```
4646
#[derive(Clone, Copy, Debug, PartialEq)]

rand_distr/src/cauchy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use rand::Rng;
4545
/// use rand_distr::{Cauchy, Distribution};
4646
///
4747
/// let cau = Cauchy::new(2.0, 5.0).unwrap();
48-
/// let v = cau.sample(&mut rand::thread_rng());
48+
/// let v = cau.sample(&mut rand::rng());
4949
/// println!("{} is from a Cauchy(2, 5) distribution", v);
5050
/// ```
5151
///

rand_distr/src/chi_squared.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use serde::{Deserialize, Serialize};
4141
/// use rand_distr::{ChiSquared, Distribution};
4242
///
4343
/// let chi = ChiSquared::new(11.0).unwrap();
44-
/// let v = chi.sample(&mut rand::thread_rng());
44+
/// let v = chi.sample(&mut rand::rng());
4545
/// println!("{} is from a χ²(11) distribution", v)
4646
/// ```
4747
#[derive(Clone, Copy, Debug, PartialEq)]

0 commit comments

Comments
 (0)