Skip to content

Commit 3bfffb5

Browse files
committed
Switch SmallRng to Pcg, and use release version of rand_core
1 parent fbd9ec3 commit 3bfffb5

8 files changed

Lines changed: 25 additions & 17 deletions

File tree

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ serde1 = ["rand_core/serde1", "rand_isaac/serde1", "rand_xorshift/serde1"] # ena
3131
members = ["rand_core", "rand_isaac", "rand_chacha", "rand_hc128", "rand_xorshift"]
3232

3333
[dependencies]
34-
rand_core = { path = "rand_core", version = "0.3", default-features = false }
34+
# Note: we can't use rand_core from a path at the same time as using rand_pcg
35+
# which uses rand_core from a crates.io release.
36+
rand_core = { version = "0.3", default-features = false }
37+
rand_pcg = { version = "0.1" }
3538
# only for deprecations and benches:
3639
rand_isaac = { path = "rand_isaac", version = "0.1" }
3740
rand_chacha = { path = "rand_chacha", version = "0.1" }

rand_chacha/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ travis-ci = { repository = "rust-random/rand" }
1919
appveyor = { repository = "rust-random/rand" }
2020

2121
[dependencies]
22-
rand_core = { path = "../rand_core", version = ">=0.2, <0.4", default-features=false }
22+
rand_core = { version = ">=0.2, <0.4", default-features=false }
2323

2424
[build-dependencies]
2525
rustc_version = "0.2"

rand_hc128/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ travis-ci = { repository = "rust-random/rand" }
1818
appveyor = { repository = "rust-random/rand" }
1919

2020
[dependencies]
21-
rand_core = { path = "../rand_core", version = ">=0.2, <0.4", default-features=false }
21+
rand_core = { version = ">=0.2, <0.4", default-features=false }

rand_isaac/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ appveyor = { repository = "rust-random/rand" }
2121
serde1 = ["serde", "serde_derive", "rand_core/serde1"]
2222

2323
[dependencies]
24-
rand_core = { path = "../rand_core", version = ">=0.2, <0.4", default-features=false }
24+
rand_core = { version = ">=0.2, <0.4", default-features=false }
2525
serde = { version = "1", optional = true }
2626
serde_derive = { version = "^1.0.38", optional = true }
2727

rand_xorshift/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ appveyor = { repository = "rust-random/rand" }
2121
serde1 = ["serde", "serde_derive"]
2222

2323
[dependencies]
24-
rand_core = { path = "../rand_core", version = ">=0.2, <0.4", default-features=false }
24+
rand_core = { version = ">=0.2, <0.4", default-features=false }
2525
serde = { version = "1", optional = true }
2626
serde_derive = { version = "^1.0.38", optional = true }
2727

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ extern crate rand_core;
245245
extern crate rand_isaac; // only for deprecations
246246
extern crate rand_chacha; // only for deprecations
247247
extern crate rand_hc128;
248+
extern crate rand_pcg;
248249
extern crate rand_xorshift;
249250

250251
#[cfg(feature = "log")] #[macro_use] extern crate log;

src/rngs/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@
4848
//!
4949
//! - [`SmallRng`] is a PRNG chosen for low memory usage, high performance and
5050
//! good statistical quality.
51-
//! The current algorithm (plain Xorshift) unfortunately performs
52-
//! poorly in statistical quality test suites (TestU01 and PractRand) and will
53-
//! be replaced in the next major release.
5451
//! - [`StdRng`] is a CSPRNG chosen for good performance and trust of security
5552
//! (based on reviews, maturity and usage). The current algorithm is HC-128,
5653
//! which is one of the recommendations by ECRYPT's eSTREAM project.

src/rngs/small.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
//! A small fast RNG
1010
1111
use {RngCore, SeedableRng, Error};
12-
use ::rand_xorshift::XorShiftRng;
12+
13+
#[cfg(all(rust_1_26, target_pointer_width = "64"))]
14+
type Rng = ::rand_pcg::Pcg64Mcg;
15+
#[cfg(not(all(rust_1_26, target_pointer_width = "64")))]
16+
type Rng = ::rand_pcg::Pcg32;
1317

1418
/// An RNG recommended when small state, cheap initialization and good
1519
/// performance are required. The PRNG algorithm in `SmallRng` is chosen to be
@@ -20,9 +24,12 @@ use ::rand_xorshift::XorShiftRng;
2024
/// future library versions may use a different internal generator with
2125
/// different output. Further, this generator may not be portable and can
2226
/// produce different output depending on the architecture. If you require
23-
/// reproducible output, use a named RNG, for example [`XorShiftRng`].
27+
/// reproducible output, use a named RNG. Refer to the documentation on the
28+
/// [`prng` module](../../prng.index.html) or the
29+
/// [small-rngs repo](https://github.com/rust-random/small-rngs).
2430
///
25-
/// The current algorithm used on all platforms is [Xorshift].
31+
/// The current algorithm is [`Pcg64Mcg`] on 64-bit platforms with Rust version
32+
/// 1.26 and later, or [`Pcg32`] otherwise.
2633
///
2734
/// # Examples
2835
///
@@ -61,10 +68,10 @@ use ::rand_xorshift::XorShiftRng;
6168
/// [`FromEntropy`]: ../trait.FromEntropy.html
6269
/// [`StdRng`]: struct.StdRng.html
6370
/// [`thread_rng`]: ../fn.thread_rng.html
64-
/// [Xorshift]: ../../rand_xorshift/struct.XorShiftRng.html
65-
/// [`XorShiftRng`]: ../../rand_xorshift/struct.XorShiftRng.html
71+
/// [`Pcg64Mcg`]: https://docs.rs/rand_pcg/0.1.0/rand_pcg/type.Pcg64Mcg.html
72+
/// [`Pcg32`]: https://docs.rs/rand_pcg/0.1.0/rand_pcg/type.Pcg32.html
6673
#[derive(Clone, Debug)]
67-
pub struct SmallRng(XorShiftRng);
74+
pub struct SmallRng(Rng);
6875

6976
impl RngCore for SmallRng {
7077
#[inline(always)]
@@ -87,13 +94,13 @@ impl RngCore for SmallRng {
8794
}
8895

8996
impl SeedableRng for SmallRng {
90-
type Seed = <XorShiftRng as SeedableRng>::Seed;
97+
type Seed = <Rng as SeedableRng>::Seed;
9198

9299
fn from_seed(seed: Self::Seed) -> Self {
93-
SmallRng(XorShiftRng::from_seed(seed))
100+
SmallRng(Rng::from_seed(seed))
94101
}
95102

96103
fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> {
97-
XorShiftRng::from_rng(rng).map(SmallRng)
104+
Rng::from_rng(rng).map(SmallRng)
98105
}
99106
}

0 commit comments

Comments
 (0)