Skip to content

Commit 1088358

Browse files
authored
Merge pull request #96 from rust-random/push-yxqmsxlmnwqq
Update for rand v0.10
2 parents f58e348 + 3be91e1 commit 1088358

16 files changed

Lines changed: 136 additions & 55 deletions

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- [Updating to 0.7](update-0.7.md)
3030
- [Updating to 0.8](update-0.8.md)
3131
- [Updating to 0.9](update-0.9.md)
32+
- [Updating to 0.10](update-0.10.md)
3233

3334
- [Contributing](contributing.md)
3435
- [Documentation](contrib-doc.md)

src/guide-data.md

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

33
```rust
4-
# extern crate rand;
5-
# use rand::RngCore;
4+
# use rand::Rng;
65
# fn main() {
76
// get some random data:
87
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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@ This section concerns theory; see also the chapter on
77
[random number generators](guide-rngs.md).
88

99
```rust
10-
# extern crate rand;
11-
# extern crate rand_chacha;
12-
use rand::{Rng, SeedableRng};
10+
use rand::{RngExt, SeedableRng};
1311

1412
# fn main() {
1513
// prepare a non-deterministic random number generator:
1614
let mut rng = rand::rng();
1715
println!("{}", rng.random::<i32>()); // prints an unknown value
1816

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

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 & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +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
31-
# extern crate rand;
32-
# extern crate rand_chacha;
3331
use rand::prelude::*;
34-
use rand_chacha::ChaCha20Rng;
32+
use rand::rngs::ChaCha20Rng;
3533
3634
fn main() {
37-
let mut rng = ChaCha20Rng::from_os_rng();
35+
let mut rng: ChaCha20Rng = rand::make_rng();
3836
println!("{}", rng.random_range(0..100));
3937
}
4038
```
@@ -47,7 +45,6 @@ Quite obviously, another RNG may be used to fill a seed. We provide a
4745
convenience method for this:
4846

4947
```rust,editable
50-
# extern crate rand;
5148
use rand::prelude::*;
5249
5350
fn main() {
@@ -60,10 +57,8 @@ But, say you want to save a key and use it later. For that you need to be a
6057
little bit more explicit:
6158

6259
```rust,editable
63-
# extern crate rand;
64-
# extern crate rand_chacha;
6560
use rand::prelude::*;
66-
use rand_chacha::ChaCha8Rng;
61+
use rand::rngs::ChaCha8Rng;
6762
6863
fn main() {
6964
let mut seed: <ChaCha8Rng as SeedableRng>::Seed = Default::default();
@@ -91,10 +86,8 @@ number while providing good bit-avalanche (so that two similar numbers such as
9186
0 and 1 translate to very different seeds and independent RNG sequences).
9287

9388
```rust,editable
94-
# extern crate rand;
95-
# extern crate rand_chacha;
9689
use rand::prelude::*;
97-
use rand_chacha::ChaCha8Rng;
90+
use rand::rngs::ChaCha8Rng;
9891
9992
fn main() {
10093
let mut rng = ChaCha8Rng::seed_from_u64(2);
@@ -150,7 +143,7 @@ function such as Argon2 must be used.
150143
[`SeedableRng::from_seed`]: https://docs.rs/rand_core/latest/rand_core/trait.SeedableRng.html#tymethod.from_seed
151144
[`SeedableRng::from_rng`]: https://docs.rs/rand_core/latest/rand_core/trait.SeedableRng.html#method.from_rng
152145
[`SeedableRng::seed_from_u64`]: https://docs.rs/rand_core/latest/rand_core/trait.SeedableRng.html#method.seed_from_u64
153-
[`SeedableRng::from_os_rng`]: https://docs.rs/rand_core/latest/rand_core/trait.SeedableRng.html#method.from_os_rng
154146
[`XorShiftRng`]: https://docs.rs/rand_xorshift/latest/rand_xorshift/struct.XorShiftRng.html
155147
[`ChaCha8Rng`]: https://docs.rs/rand_chacha/latest/rand_chacha/struct.ChaCha8Rng.html
156148
[`rand_seeder`]: https://github.com/rust-random/seeder/
149+
[`rand::make_rng()`]: https://docs.rs/rand/latest/rand/fn.make_rng.html

src/guide-start.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ cargo add rand --features small_rng
1111

1212
Now, paste the following into `src/main.rs`:
1313
```rust
14-
# extern crate rand;
1514
use rand::prelude::*;
1615

1716
fn main() {

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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ Finally, it has a function to sample from arbitrary distributions:
3838
Examples:
3939

4040
```rust
41-
# extern crate rand;
42-
use rand::Rng;
41+
use rand::RngExt;
4342
# fn main() {
4443
let mut rng = rand::rng();
4544

@@ -58,7 +57,6 @@ println!("roll = {}", rng.random_range(1..=6));
5857

5958
Additionally, the [`random`] function is a short-cut to [`Rng::random`] on the [`rng()`]:
6059
```rust
61-
# extern crate rand;
6260
# use rand::Rng;
6361
# fn main() {
6462
println!("Tossing a coin...");
@@ -92,8 +90,7 @@ according to reasonable logic:
9290

9391
Given that, we can implement the [`StandardUniform`] distribution for our own types:
9492
```rust
95-
# extern crate rand;
96-
use rand::Rng;
93+
use rand::{Rng, RngExt};
9794
use rand::distr::{Distribution, StandardUniform, Uniform};
9895
use std::f64::consts::TAU; // = 2π
9996

0 commit comments

Comments
 (0)