Skip to content

Commit 36d65c8

Browse files
committed
Switch SmallRng to Pcg, and use release version of rand_core
1 parent 2457cdb commit 36d65c8

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ members = ["rand_core", "rand_isaac", "rand_chacha", "rand_hc128", "rand_pcg", "
3232

3333
[dependencies]
3434
rand_core = { path = "rand_core", version = "0.3", default-features = false }
35+
rand_pcg = { path = "rand_pcg", version = "0.1" }
3536
# only for deprecations and benches:
3637
rand_isaac = { path = "rand_isaac", version = "0.1" }
3738
rand_chacha = { path = "rand_chacha", version = "0.1" }

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)