1 // Copyright 2018 Developers of the Rand project.
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.
11 use rand_core
::{Error, RngCore, SeedableRng}
;
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
;
18 /// A small-state, fast non-crypto PRNG
20 /// `SmallRng` may be a good choice when a PRNG with small state, cheap
21 /// initialization, good statistical quality and good performance are required.
22 /// It is **not** a good choice when security against prediction or
23 /// reproducibility are important.
25 /// This PRNG is **feature-gated**: to use, you must enable the crate feature
28 /// The algorithm is deterministic but should not be considered reproducible
29 /// due to dependence on platform and possible replacement in future
30 /// library versions. For a reproducible generator, use a named PRNG from an
31 /// external crate, e.g. [rand_pcg] or [rand_chacha].
32 /// Refer also to [The Book](https://rust-random.github.io/book/guide-rngs.html).
34 /// The PRNG algorithm in `SmallRng` is chosen to be
35 /// efficient on the current platform, without consideration for cryptography
36 /// or security. The size of its state is much smaller than [`StdRng`].
37 /// The current algorithm is [`Pcg64Mcg`](rand_pcg::Pcg64Mcg) on 64-bit
38 /// platforms and [`Pcg32`](rand_pcg::Pcg32) on 32-bit platforms. Both are
39 /// implemented by the [rand_pcg] crate.
43 /// Initializing `SmallRng` with a random seed can be done using [`SeedableRng::from_entropy`]:
46 /// use rand::{Rng, SeedableRng};
47 /// use rand::rngs::SmallRng;
49 /// // Create small, cheap to initialize and fast RNG with a random seed.
50 /// // The randomness is supplied by the operating system.
51 /// let mut small_rng = SmallRng::from_entropy();
52 /// # let v: u32 = small_rng.gen();
55 /// When initializing a lot of `SmallRng`'s, using [`thread_rng`] can be more
59 /// use rand::{SeedableRng, thread_rng};
60 /// use rand::rngs::SmallRng;
62 /// // Create a big, expensive to initialize and slower, but unpredictable RNG.
63 /// // This is cached and done only once per thread.
64 /// let mut thread_rng = thread_rng();
65 /// // Create small, cheap to initialize and fast RNGs with random seeds.
66 /// // One can generally assume this won't fail.
67 /// let rngs: Vec<SmallRng> = (0..10)
68 /// .map(|_| SmallRng::from_rng(&mut thread_rng).unwrap())
72 /// [`StdRng`]: crate::rngs::StdRng
73 /// [`thread_rng`]: crate::thread_rng
74 /// [rand_chacha]: https://crates.io/crates/rand_chacha
75 /// [rand_pcg]: https://crates.io/crates/rand_pcg
76 #[derive(Clone, Debug)]
77 pub struct SmallRng(Rng
);
79 impl RngCore
for SmallRng
{
81 fn next_u32(&mut self) -> u32 {
86 fn next_u64(&mut self) -> u64 {
91 fn fill_bytes(&mut self, dest
: &mut [u8]) {
92 self.0.fill_bytes
(dest
);
96 fn try_fill_bytes(&mut self, dest
: &mut [u8]) -> Result
<(), Error
> {
97 self.0.try_fill_bytes(dest
)
101 impl SeedableRng
for SmallRng
{
102 type Seed
= <Rng
as SeedableRng
>::Seed
;
105 fn from_seed(seed
: Self::Seed
) -> Self {
106 SmallRng(Rng
::from_seed(seed
))
110 fn from_rng
<R
: RngCore
>(rng
: R
) -> Result
<Self, Error
> {
111 Rng
::from_rng(rng
).map(SmallRng
)