Struct rand::rngs::SmallRng

source ·
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

source

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());
source

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.

source

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 Clone for SmallRng

source§

fn clone(&self) -> SmallRng

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for SmallRng

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq for SmallRng

source§

fn eq(&self, other: &SmallRng) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl RngCore for SmallRng

source§

fn next_u32(&mut self) -> u32

Return the next random u32. Read more
source§

fn next_u64(&mut self) -> u64

Return the next random u64. Read more
source§

fn fill_bytes(&mut self, dest: &mut [u8])

Fill dest with random data. Read more
source§

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>

Fill dest entirely with random data. Read more
source§

fn read_adapter(&mut self) -> RngReadAdapter<'_, Self>
where Self: Sized,

Convert an RngCore to a RngReadAdapter.
source§

impl Eq for SmallRng

source§

impl StructuralPartialEq for SmallRng

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<R> Rng for R
where R: RngCore + ?Sized,

source§

fn gen<T>(&mut self) -> T

Return a random value via the Standard distribution. Read more
source§

fn gen_range<T, R>(&mut self, range: R) -> T
where T: SampleUniform, R: SampleRange<T>,

Generate a random value in the given range. Read more
source§

fn gen_iter<T>(self) -> DistIter<Standard, Self, T>
where Self: Sized, Standard: Distribution<T>,

Generate values via an iterator Read more
source§

fn sample<T, D: Distribution<T>>(&mut self, distr: D) -> T

Sample a new value, using the given distribution. Read more
source§

fn sample_iter<T, D>(self, distr: D) -> DistIter<D, Self, T>
where D: Distribution<T>, Self: Sized,

Create an iterator that generates values using the given distribution. Read more
source§

fn fill<T: Fill + ?Sized>(&mut self, dest: &mut T)

Fill any type implementing Fill with random data Read more
source§

fn try_fill<T: Fill + ?Sized>(&mut self, dest: &mut T) -> Result<(), Error>

Fill any type implementing Fill with random data Read more
source§

fn gen_bool(&mut self, p: f64) -> bool

Return a bool with a probability p of being true. Read more
source§

fn gen_ratio(&mut self, numerator: u32, denominator: u32) -> bool

Return a bool with a probability of 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
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V