pub struct SmallRng(/* private fields */);
Expand description
A small-state, fast non-crypto PRNG
SmallRng
may be a good choice when a PRNG with small state, cheap
initialization, good statistical quality and good performance are required.
Note that depending on the application, StdRng
may be faster on many
modern platforms while providing higher-quality randomness. Furthermore,
SmallRng
is not a good choice when:
- Portability is required. Its implementation is not fixed. Use a named generator from an external crate instead, for example rand_xoshiro or rand_chacha. Refer also to The Book.
- Security against prediction is important. Use
StdRng
instead.
The PRNG algorithm in SmallRng
is chosen to be efficient on the current
platform, without consideration for cryptography or security. The size of
its state is much smaller than StdRng
. The current algorithm is
Xoshiro256PlusPlus
on 64-bit platforms and Xoshiro128PlusPlus
on 32-bit
platforms. Both are also implemented by the rand_xoshiro crate.
Implementations§
source§impl SmallRng
impl SmallRng
sourcepub fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error>
pub fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error>
Construct an instance seeded from another Rng
We recommend that the source (master) RNG uses a different algorithm
(i.e. is not SmallRng
) to avoid correlations between the child PRNGs.
§Example
let rng = SmallRng::from_rng(rand::thread_rng());
sourcepub fn from_thread_rng() -> Self
pub fn from_thread_rng() -> Self
Construct an instance seeded from the thread-local RNG
§Panics
This method panics only if thread_rng
fails to
initialize.
sourcepub fn seed_from_u64(state: u64) -> Self
pub fn seed_from_u64(state: u64) -> Self
Construct an instance from a u64
seed
This provides a convenient method of seeding a SmallRng
from a simple
number by use of another algorithm to mutate and expand the input.
This is suitable for use with low Hamming Weight numbers like 0 and 1.
Warning: the implementation is deterministic but not portable: output values may differ according to platform and may be changed by a future version of the library.
§Example
let rng = SmallRng::seed_from_u64(1);
Trait Implementations§
source§impl PartialEq for SmallRng
impl PartialEq for SmallRng
source§impl RngCore for SmallRng
impl RngCore for SmallRng
source§fn fill_bytes(&mut self, dest: &mut [u8])
fn fill_bytes(&mut self, dest: &mut [u8])
dest
with random data. Read moresource§fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>
dest
entirely with random data. Read moresource§fn read_adapter(&mut self) -> RngReadAdapter<'_, Self>where
Self: Sized,
fn read_adapter(&mut self) -> RngReadAdapter<'_, Self>where
Self: Sized,
RngCore
to a RngReadAdapter
.impl Eq for SmallRng
impl StructuralPartialEq for SmallRng
Auto Trait Implementations§
impl Freeze for SmallRng
impl RefUnwindSafe for SmallRng
impl Send for SmallRng
impl Sync for SmallRng
impl Unpin for SmallRng
impl UnwindSafe for SmallRng
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<R> Rng for R
impl<R> Rng for R
source§fn gen<T>(&mut self) -> Twhere
Standard: Distribution<T>,
fn gen<T>(&mut self) -> Twhere
Standard: Distribution<T>,
source§fn gen_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
fn gen_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
source§fn gen_iter<T>(self) -> DistIter<Standard, Self, T> ⓘ
fn gen_iter<T>(self) -> DistIter<Standard, Self, T> ⓘ
source§fn sample<T, D: Distribution<T>>(&mut self, distr: D) -> T
fn sample<T, D: Distribution<T>>(&mut self, distr: D) -> T
source§fn sample_iter<T, D>(self, distr: D) -> DistIter<D, Self, T> ⓘwhere
D: Distribution<T>,
Self: Sized,
fn sample_iter<T, D>(self, distr: D) -> DistIter<D, Self, T> ⓘwhere
D: Distribution<T>,
Self: Sized,
source§fn gen_bool(&mut self, p: f64) -> bool
fn gen_bool(&mut self, p: f64) -> bool
p
of being true. Read moresource§fn gen_ratio(&mut self, numerator: u32, denominator: u32) -> bool
fn gen_ratio(&mut self, numerator: u32, denominator: u32) -> bool
numerator/denominator
of being
true. I.e. gen_ratio(2, 3)
has chance of 2 in 3, or about 67%, of
returning true. If numerator == denominator
, then the returned value
is guaranteed to be true
. If numerator == 0
, then the returned
value is guaranteed to be false
. Read more