Struct SmallRng
pub struct SmallRng(/* private field */);
A small-state, fast, non-crypto, non-portable PRNG
This is the "standard small" RNG, a generator with the following properties:
- Non-portable: any future library version may replace the algorithm and results may be platform-dependent. (For a small portable generator, use the rand_pcg or rand_xoshiro crate.)
- Non-cryptographic: output is easy to predict (insecure)
- Quality: statistically good quality
- Fast: the RNG is fast for both bulk generation and single values, with consistent cost of method calls
- Fast initialization
- Small state: little memory usage (current state size is 16-32 bytes depending on platform)
The current algorithm is
Xoshiro256PlusPlus on 64-bit platforms and Xoshiro128PlusPlus on 32-bit
platforms. Both are also implemented by the rand_xoshiro crate.
Seeding (construction)
This generator implements the SeedableRng trait. All methods are
suitable for seeding, but note that, even with a fixed seed, output is not
portable. Some suggestions:
- To automatically seed with a unique seed, use
rand::make_rng():use SmallRng; let mut rng: SmallRng = make_rng; # let _ = next_u32; - To use a deterministic integral seed, use
seed_from_u64. This uses a hash function internally to yield a (typically) good seed from any input.# use ; let rng = seed_from_u64; # let _: SmallRng = rng; - To seed deterministically from text or other input, use
rand_seeder.
See also Seeding RNGs in the book.
Generation
The generator implements Rng and thus also RngExt.
See also the Random Values chapter in the book.
Trait Implementations
impl Clone for SmallRng
fn clone(&self) -> SmallRng
impl Debug for SmallRng
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Eq for SmallRng
impl PartialEq for SmallRng
fn eq(&self, other: &SmallRng) -> bool
impl SeedableRng for SmallRng
type Seed = [u8; 32];fn from_seed(seed: Self::Seed) -> Selffn seed_from_u64(state: u64) -> Self
impl StructuralPartialEq for SmallRng
impl TryRng for SmallRng
type Error = Infallible;fn try_next_u32(&mut self) -> Result<u32, Infallible>fn try_next_u64(&mut self) -> Result<u64, Infallible>fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Infallible>
Auto Trait Implementations
impl Freeze for SmallRng
impl RefUnwindSafe for SmallRng
impl Send for SmallRng
impl Sync for SmallRng
impl Unpin for SmallRng
impl UnsafeUnpin for SmallRng
impl UnwindSafe for SmallRng
Blanket Implementations
impl<R> Rng for SmallRng
where
R: TryRng<Error = Infallible> + ?Sized,
fn next_u32(&mut self) -> u32fn next_u64(&mut self) -> u64fn fill_bytes(&mut self, dst: &mut [u8])
impl<R> RngCore for SmallRng
where
R: Rng,
impl<R> RngExt for SmallRng
where
R: Rng + ?Sized,
impl<R> TryRngCore for SmallRng
where
R: TryRng,
type Error = <R as TryRng>::Error;
impl<T> Any for SmallRng
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for SmallRng
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for SmallRng
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for SmallRng
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> From<T> for SmallRng
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for SmallRng
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T, U> Into<U> for SmallRng
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for SmallRng
where
U: Into<T>,
type Error = Infallible;fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto<U> for SmallRng
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>