Skip to content

Commit 3be91e1

Browse files
committed
Updates for rand v0.10
1 parent e0ae07a commit 3be91e1

10 files changed

Lines changed: 34 additions & 25 deletions

src/guide-data.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Random data
22

33
```rust
4-
# use rand::RngCore;
4+
# use rand::Rng;
55
# fn main() {
66
// get some random data:
77
let mut data = [0u8; 8];

src/guide-dist.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Lets go over the distributions by type:
9494
- For enums, you have to implement uniform sampling yourself. For example, you
9595
could use the following approach:
9696
```rust,noplayground
97-
# use rand::{Rng, distr::{Distribution, StandardUniform}};
97+
# use rand::{Rng, RngExt, distr::{Distribution, StandardUniform}};
9898
pub enum Food {
9999
Burger,
100100
Pizza,

src/guide-gen.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ This section concerns theory; see also the chapter on
77
[random number generators](guide-rngs.md).
88

99
```rust
10-
use rand::{Rng, SeedableRng};
10+
use rand::{RngExt, SeedableRng};
1111

1212
# fn main() {
1313
// prepare a non-deterministic random number generator:
1414
let mut rng = rand::rng();
1515
println!("{}", rng.random::<i32>()); // prints an unknown value
1616

1717
// prepare a deterministic generator:
18-
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(123);
18+
let mut rng = rand::rngs::ChaCha8Rng::seed_from_u64(123);
1919
println!("{}", rng.random::<i32>()); // prints -416273517
2020
# }
2121
```

src/guide-parallel.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ non-deterministic simulation too.
108108

109109
```rust
110110
use rand::distr::{Distribution, Uniform};
111-
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
111+
use rand::{SeedableRng, rngs::ChaCha8Rng};
112112
use rayon::prelude::*;
113113

114114
static SEED: u64 = 0;

src/guide-rngs.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,17 @@ seeded with a secure random key. Should the key be known or guessable, all
247247
output of the CSPRNG is easy to guess. This implies that the seed should
248248
come from a trusted source; usually either the OS or another CSPRNG. For this
249249
purpose, we recommend using the [`getrandom`] crate which interfaces the OS's
250-
secure random interface. [`SeedableRng::from_os_rng`] is a wrapper around
251-
[`getrandom`] for convenience. Alternatively, using a user-space CSPRNG such as
252-
[`ThreadRng`] for seeding should be sufficient.
250+
secure random interface. Alternatively, using a user-space CSPRNG such as
251+
[`rand::make_rng()`] or [`ThreadRng`] for seeding should be sufficient. In code:
252+
```rust
253+
use rand::{rngs::ChaCha12Rng, rngs::SysRng, SeedableRng};
254+
255+
/// Seed explicitly from SysRng:
256+
let mut rng1 = ChaCha12Rng::try_from_rng(&mut SysRng).unwrap();
257+
258+
/// Seed from ThreadRng (or SysRng if ThreadRng is not available):
259+
let mut rng2: ChaCha12Rng = rand::make_rng();
260+
```
253261

254262
Further, it should be obvious that the internal state of a CSPRNG must be
255263
kept secret. With that in mind, our implementations do not provide direct
@@ -335,5 +343,5 @@ by P. Hellekalek.
335343
[NIST]: https://www.nist.gov/
336344
[ECRYPT]: http://www.ecrypt.eu.org/
337345
[`getrandom`]: https://docs.rs/getrandom/
338-
[`SeedableRng::from_os_rng`]: https://docs.rs/rand/latest/rand/trait.SeedableRng.html#method.from_os_rng
339346
[zeroize]: https://crates.io/crates/zeroize
347+
[`rand::make_rng()`]: https://docs.rs/rand/latest/rand/fn.make_rng.html

src/guide-seeding.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ PRNGs may be seeded directly from such a value with [`SeedableRng::from_seed`].
2525

2626
### Fresh entropy
2727

28-
Using a fresh seed (direct from the OS) is easy using [`SeedableRng::from_os_rng`]:
28+
Using a fresh seed is easy using [`rand::make_rng()`]:
2929

3030
```rust,editable
3131
use rand::prelude::*;
32-
use rand_chacha::ChaCha20Rng;
32+
use rand::rngs::ChaCha20Rng;
3333
3434
fn main() {
35-
let mut rng = ChaCha20Rng::from_os_rng();
35+
let mut rng: ChaCha20Rng = rand::make_rng();
3636
println!("{}", rng.random_range(0..100));
3737
}
3838
```
@@ -58,7 +58,7 @@ little bit more explicit:
5858

5959
```rust,editable
6060
use rand::prelude::*;
61-
use rand_chacha::ChaCha8Rng;
61+
use rand::rngs::ChaCha8Rng;
6262
6363
fn main() {
6464
let mut seed: <ChaCha8Rng as SeedableRng>::Seed = Default::default();
@@ -87,7 +87,7 @@ number while providing good bit-avalanche (so that two similar numbers such as
8787

8888
```rust,editable
8989
use rand::prelude::*;
90-
use rand_chacha::ChaCha8Rng;
90+
use rand::rngs::ChaCha8Rng;
9191
9292
fn main() {
9393
let mut rng = ChaCha8Rng::seed_from_u64(2);
@@ -143,7 +143,7 @@ function such as Argon2 must be used.
143143
[`SeedableRng::from_seed`]: https://docs.rs/rand_core/latest/rand_core/trait.SeedableRng.html#tymethod.from_seed
144144
[`SeedableRng::from_rng`]: https://docs.rs/rand_core/latest/rand_core/trait.SeedableRng.html#method.from_rng
145145
[`SeedableRng::seed_from_u64`]: https://docs.rs/rand_core/latest/rand_core/trait.SeedableRng.html#method.seed_from_u64
146-
[`SeedableRng::from_os_rng`]: https://docs.rs/rand_core/latest/rand_core/trait.SeedableRng.html#method.from_os_rng
147146
[`XorShiftRng`]: https://docs.rs/rand_xorshift/latest/rand_xorshift/struct.XorShiftRng.html
148147
[`ChaCha8Rng`]: https://docs.rs/rand_chacha/latest/rand_chacha/struct.ChaCha8Rng.html
149148
[`rand_seeder`]: https://github.com/rust-random/seeder/
149+
[`rand::make_rng()`]: https://docs.rs/rand/latest/rand/fn.make_rng.html

src/guide-test-fn-rng.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
Occasionally a function that uses random number generators might need to be tested. For functions that need to be tested with test vectors, the following approach might be adapted:
44

55
```rust
6-
use rand::{TryCryptoRng, rngs::OsRng};
6+
use rand::{TryCryptoRng, rngs::SysRng};
77

8-
pub struct CryptoOperations<R: TryCryptoRng = OsRng> {
8+
pub struct CryptoOperations<R: TryCryptoRng = SysRng> {
99
rng: R
1010
}
1111

@@ -30,7 +30,7 @@ impl<R: TryCryptoRng> CryptoOperations<R> {
3030
}
3131

3232
fn main() {
33-
let rng = OsRng;
33+
let rng = SysRng;
3434
let mut crypto_ops = <CryptoOperations>::new(rng);
3535

3636
let mut secret: [u8; 8] = *b"\x00\x01\x02\x03\x04\x05\x06\x07";

src/guide-values.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Finally, it has a function to sample from arbitrary distributions:
3838
Examples:
3939

4040
```rust
41-
use rand::Rng;
41+
use rand::RngExt;
4242
# fn main() {
4343
let mut rng = rand::rng();
4444

@@ -90,7 +90,7 @@ according to reasonable logic:
9090

9191
Given that, we can implement the [`StandardUniform`] distribution for our own types:
9292
```rust
93-
use rand::Rng;
93+
use rand::{Rng, RngExt};
9494
use rand::distr::{Distribution, StandardUniform, Uniform};
9595
use std::f64::consts::TAU; // = 2π
9696

src/quick-start.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ See the [RNGs] section for more RNGs, but avoid `SmallRng` and `StdRng` if you
8181
care about reproducible results.
8282

8383
```rust,editable
84-
use rand::{Rng, SeedableRng};
84+
use rand::{rngs::ChaCha8Rng, RngExt, SeedableRng};
8585
8686
fn main() {
87-
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(10);
87+
let mut rng = ChaCha8Rng::seed_from_u64(10);
8888
println!("Random f32: {}", rng.random::<f32>());
8989
}
9090
```

src/update-0.10.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Users of `rand` will often need to import `rand::RngExt` may need to migrate fro
2626

2727
`rand_core::OsRng` has been replaced with `getrandom::SysRng` (also available as `rand::rngs::SysRng`).
2828

29-
The methods `SeedableRng::from_os_rng` and `try_from_os_rng` have thus been removed. `rand::make_rng()` is provided as a partial replacement; otherwise use `SomeRng::try_from_rng(&mut SysRng).unwrap()`.
29+
The methods `SeedableRng::from_os_rng` and `try_from_os_rng` have thus been removed. [`rand::make_rng()`] is provided as a partial replacement; otherwise use `SomeRng::try_from_rng(&mut SysRng).unwrap()`.
3030

3131

3232
## PRNGs
@@ -41,10 +41,10 @@ Other PRNG crates have been updated with minimal changes (though this may not re
4141

4242
`StdRng` and `ChaCha{8,12,20}Rng` no longer implement `Clone` or the [serde] traits. This was a deliberate choice to prevent accidental key-stream duplication or persisting to external storage. Note that it remains possible to clone or serialize these RNGs by reconstructing a new instance with the same key, then setting the stream (if applicable) and word position. For example:
4343
```rust,editable
44-
use rand::{rngs::ChaCha8Rng, Rng};
44+
use rand::{rngs::ChaCha8Rng, Rng, SeedableRng};
4545
4646
let mut rng1: ChaCha8Rng = rand::make_rng();
47-
let _: u128 = rng1.next_u64();
47+
let _ = rng1.next_u64();
4848
4949
let mut rng2 = ChaCha8Rng::from_seed(rng1.get_seed());
5050
rng2.set_stream(rng1.get_stream());
@@ -85,3 +85,4 @@ There are no known value-breaking changes to `rand` in v0.10.
8585

8686

8787
[serde]: https://serde.rs/
88+
[`rand::make_rng()`]: https://docs.rs/rand/latest/rand/fn.make_rng.html

0 commit comments

Comments
 (0)