Skip to content

Commit 713d190

Browse files
committed
SmallRng: Replace PCG algorithm with xoshiro{128,256}++
Due to close correlations of PCG streams (rust-random#907) and lack of right-state propegation (rust-random#905), the `SmallRng` algorithm is switched to xoshiro{128,256}++. The implementation is taken from the `rand_xoshiro` crate and slightly simplified. Fixes rust-random#910.
1 parent 54b77d8 commit 713d190

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ simd_support = ["packed_simd"]
4343
std_rng = ["rand_chacha", "rand_hc"]
4444

4545
# Option: enable SmallRng
46-
small_rng = ["rand_pcg"]
46+
small_rng = []
4747

4848
[workspace]
4949
members = [
@@ -56,7 +56,6 @@ members = [
5656

5757
[dependencies]
5858
rand_core = { path = "rand_core", version = "0.5.1" }
59-
rand_pcg = { path = "rand_pcg", version = "0.2.1", optional = true }
6059
log = { version = "0.4.4", optional = true }
6160
serde = { version = "1.0.103", features = ["derive"], optional = true }
6261

src/rngs/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,13 @@
101101

102102
pub mod mock; // Public so we don't export `StepRng` directly, making it a bit
103103
// more clear it is intended for testing.
104+
105+
#[cfg(all(feature = "small_rng", target_pointer_width = "64"))]
106+
mod xoshiro256plusplus;
107+
#[cfg(all(feature = "small_rng", not(target_pointer_width = "64")))]
108+
mod xoshiro128plusplus;
104109
#[cfg(feature = "small_rng")] mod small;
110+
105111
#[cfg(feature = "std_rng")] mod std;
106112
#[cfg(all(feature = "std", feature = "std_rng"))] pub(crate) mod thread;
107113

src/rngs/small.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
1111
use rand_core::{Error, RngCore, SeedableRng};
1212

13-
#[cfg(all(not(target_os = "emscripten"), target_pointer_width = "64"))]
14-
type Rng = rand_pcg::Pcg64Mcg;
15-
#[cfg(not(all(not(target_os = "emscripten"), target_pointer_width = "64")))]
16-
type Rng = rand_pcg::Pcg32;
13+
#[cfg(target_pointer_width = "64")]
14+
type Rng = super::xoshiro256plusplus::Xoshiro256PlusPlus;
15+
#[cfg(not(target_pointer_width = "64"))]
16+
type Rng = super::xoshiro128plusplus::Xoshiro128PlusPlus;
1717

1818
/// A small-state, fast non-crypto PRNG
1919
///
@@ -25,15 +25,14 @@ type Rng = rand_pcg::Pcg32;
2525
/// The algorithm is deterministic but should not be considered reproducible
2626
/// due to dependence on platform and possible replacement in future
2727
/// library versions. For a reproducible generator, use a named PRNG from an
28-
/// external crate, e.g. [rand_pcg] or [rand_chacha].
28+
/// external crate, e.g. [rand_xoshiro] or [rand_chacha].
2929
/// Refer also to [The Book](https://rust-random.github.io/book/guide-rngs.html).
3030
///
31-
/// The PRNG algorithm in `SmallRng` is chosen to be
32-
/// efficient on the current platform, without consideration for cryptography
33-
/// or security. The size of its state is much smaller than [`StdRng`].
34-
/// The current algorithm is [`Pcg64Mcg`](rand_pcg::Pcg64Mcg) on 64-bit
35-
/// platforms and [`Pcg32`](rand_pcg::Pcg32) on 32-bit platforms. Both are
36-
/// implemented by the [rand_pcg] crate.
31+
/// The PRNG algorithm in `SmallRng` is chosen to be efficient on the current
32+
/// platform, without consideration for cryptography or security. The size of
33+
/// its state is much smaller than [`StdRng`]. The current algorithm is
34+
/// `Xoshiro256PlusPlus` on 64-bit platforms and `Xoshiro128PlusPlus` on 32-bit
35+
/// platforms. Both are implemented by the [rand_xoshiro] crate.
3736
///
3837
/// # Examples
3938
///
@@ -69,7 +68,7 @@ type Rng = rand_pcg::Pcg32;
6968
/// [`StdRng`]: crate::rngs::StdRng
7069
/// [`thread_rng`]: crate::thread_rng
7170
/// [rand_chacha]: https://crates.io/crates/rand_chacha
72-
/// [rand_pcg]: https://crates.io/crates/rand_pcg
71+
/// [rand_xoshiro]: https://crates.io/crates/rand_pcg
7372
#[cfg_attr(doc_cfg, doc(cfg(feature = "small_rng")))]
7473
#[derive(Clone, Debug, PartialEq, Eq)]
7574
pub struct SmallRng(Rng);

0 commit comments

Comments
 (0)