rand/rngs/small.rs
1// Copyright 2018 Developers of the Rand project.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! A small fast RNG
10
11use rand_core::{RngCore, SeedableRng};
12
13#[cfg(any(target_pointer_width = "32", target_pointer_width = "16"))]
14type Rng = super::xoshiro128plusplus::Xoshiro128PlusPlus;
15#[cfg(target_pointer_width = "64")]
16type Rng = super::xoshiro256plusplus::Xoshiro256PlusPlus;
17
18/// A small-state, fast, non-crypto, non-portable PRNG
19///
20/// This is the "standard small" RNG, a generator with the following properties:
21///
22/// - Non-[portable]: any future library version may replace the algorithm
23/// and results may be platform-dependent.
24/// (For a small portable generator, use the [rand_pcg] or [rand_xoshiro] crate.)
25/// - Non-cryptographic: output is easy to predict (insecure)
26/// - [Quality]: statistically good quality
27/// - Fast: the RNG is fast for both bulk generation and single values, with
28/// consistent cost of method calls
29/// - Fast initialization
30/// - Small state: little memory usage (current state size is 16-32 bytes
31/// depending on platform)
32///
33/// The current algorithm is
34/// `Xoshiro256PlusPlus` on 64-bit platforms and `Xoshiro128PlusPlus` on 32-bit
35/// platforms. Both are also implemented by the [rand_xoshiro] crate.
36///
37/// ## Seeding (construction)
38///
39/// This generator implements the [`SeedableRng`] trait. All methods are
40/// suitable for seeding, but note that, even with a fixed seed, output is not
41/// [portable]. Some suggestions:
42///
43/// 1. To automatically seed with a unique seed, use [`SeedableRng::from_rng`]:
44/// ```
45/// use rand::SeedableRng;
46/// use rand::rngs::SmallRng;
47/// let rng = SmallRng::from_rng(&mut rand::rng());
48/// # let _: SmallRng = rng;
49/// ```
50/// 2. To use a deterministic integral seed, use `seed_from_u64`. This uses a
51/// hash function internally to yield a (typically) good seed from any
52/// input.
53/// ```
54/// # use rand::{SeedableRng, rngs::SmallRng};
55/// let rng = SmallRng::seed_from_u64(1);
56/// # let _: SmallRng = rng;
57/// ```
58/// 3. To seed deterministically from text or other input, use [`rand_seeder`].
59///
60/// See also [Seeding RNGs] in the book.
61///
62/// ## Generation
63///
64/// The generators implements [`RngCore`] and thus also [`Rng`][crate::Rng].
65/// See also the [Random Values] chapter in the book.
66///
67/// [portable]: https://rust-random.github.io/book/crate-reprod.html
68/// [Seeding RNGs]: https://rust-random.github.io/book/guide-seeding.html
69/// [Random Values]: https://rust-random.github.io/book/guide-values.html
70/// [Quality]: https://rust-random.github.io/book/guide-rngs.html#quality
71/// [`StdRng`]: crate::rngs::StdRng
72/// [rand_pcg]: https://crates.io/crates/rand_pcg
73/// [rand_xoshiro]: https://crates.io/crates/rand_xoshiro
74/// [`rand_chacha::ChaCha8Rng`]: https://docs.rs/rand_chacha/latest/rand_chacha/struct.ChaCha8Rng.html
75/// [`rand_seeder`]: https://docs.rs/rand_seeder/latest/rand_seeder/
76#[derive(Clone, Debug, PartialEq, Eq)]
77pub struct SmallRng(Rng);
78
79impl SeedableRng for SmallRng {
80 // Fix to 256 bits. Changing this is a breaking change!
81 type Seed = [u8; 32];
82
83 #[inline(always)]
84 fn from_seed(seed: Self::Seed) -> Self {
85 // This is for compatibility with 32-bit platforms where Rng::Seed has a different seed size
86 // With MSRV >= 1.77: let seed = *seed.first_chunk().unwrap()
87 const LEN: usize = core::mem::size_of::<<Rng as SeedableRng>::Seed>();
88 let seed = (&seed[..LEN]).try_into().unwrap();
89 SmallRng(Rng::from_seed(seed))
90 }
91
92 #[inline(always)]
93 fn seed_from_u64(state: u64) -> Self {
94 SmallRng(Rng::seed_from_u64(state))
95 }
96}
97
98impl RngCore for SmallRng {
99 #[inline(always)]
100 fn next_u32(&mut self) -> u32 {
101 self.0.next_u32()
102 }
103
104 #[inline(always)]
105 fn next_u64(&mut self) -> u64 {
106 self.0.next_u64()
107 }
108
109 #[inline(always)]
110 fn fill_bytes(&mut self, dest: &mut [u8]) {
111 self.0.fill_bytes(dest)
112 }
113}