Trait RngCore
trait RngCore
Implementation-level interface for RNGs
This trait encapsulates the low-level functionality common to all
generators, and is the "back end", to be implemented by generators.
End users should normally use the rand::Rng trait
which is automatically implemented for every type implementing RngCore.
Three different methods for generating random data are provided since the
optimal implementation of each is dependent on the type of generator. There
is no required relationship between the output of each; e.g. many
implementations of fill_bytes consume a whole number of u32 or u64
values and drop any remaining unused bytes. The same can happen with the
next_u32 and next_u64 methods, implementations may discard some
random bits for efficiency.
Implementers should produce bits uniformly. Pathological RNGs (e.g. always
returning the same value, or never setting certain bits) can break rejection
sampling used by random distributions, and also break other RNGs when
seeding them via SeedableRng::from_rng.
Algorithmic generators implementing SeedableRng should normally have
portable, reproducible output, i.e. fix Endianness when converting values
to avoid platform differences, and avoid making any changes which affect
output (except by communicating that the release has breaking changes).
Typically an RNG will implement only one of the methods available
in this trait directly, then use the helper functions from the
impls module to implement the other methods.
Note that implementors of RngCore also automatically implement
the TryRngCore trait with the Error associated type being
equal to Infallible.
It is recommended that implementations also implement:
Debugwith a custom implementation which does not print any internal state (at least,CryptoRngs should not risk leaking state throughDebug).SerializeandDeserialize(from Serde), preferably making Serde support optional at the crate level in PRNG libs.Clone, if possible.- never implement
Copy(accidental copies may cause repeated values). - do not implement
Defaultfor pseudorandom generators, but instead implementSeedableRng, to guide users towards proper seeding. External / hardware RNGs can choose to implementDefault. EqandPartialEqcould be implemented, but are probably not useful.
Example
A simple example, obviously not generating very random output:
use ;
;
Required Methods
fn next_u32(self: &mut Self) -> u32Return the next random
u32.RNGs must implement at least one method from this trait directly. In the case this method is not implemented directly, it can be implemented using
self.next_u64() as u32or viaimpls::next_u32_via_fill.fn next_u64(self: &mut Self) -> u64Return the next random
u64.RNGs must implement at least one method from this trait directly. In the case this method is not implemented directly, it can be implemented via
impls::next_u64_via_u32or viaimpls::next_u64_via_fill.fn fill_bytes(self: &mut Self, dst: &mut [u8])Fill
destwith random data.RNGs must implement at least one method from this trait directly. In the case this method is not implemented directly, it can be implemented via
impls::fill_bytes_via_next.This method should guarantee that
destis entirely filled with new data, and may panic if this is impossible (e.g. reading past the end of a file that is being used as the source of randomness).
Implementors
impl<R: BlockRngCore<Item = u32>> RngCore for BlockRng<R>impl<T: DerefMut> RngCore for Timpl<R: TryRngCore> RngCore for UnwrapErr<R>impl<R: BlockRngCore<Item = u64>> RngCore for BlockRng64<R>