Trait RandomnessSource

Source
pub trait RandomnessSource: Default {
    type TNumber: Number;

    // Required method
    fn next_number(&mut self) -> Self::TNumber;

    // Provided method
    fn fill_bytes(&mut self, bytes: &mut [u8]) { ... }
}
Expand description

Simple trait for randomness source.

This looks (and in fact is) the same as PseudoRandomNumberGenerator, however it is distinguished by the way it works. It is allowed for implementors of this trait to be inefficient, but have potentially better randomness (e.g. by reading from appropriate hardware source).

It is also a good idea to seed PseudoRandomNumberGenerator objects with whatever RandomnessSource returns.

In its essence this is the same as PseudoRandomNumberGenerator. We distinguish those types mostly for type safety. These have different purposes.

Required Associated Types§

Required Methods§

Source

fn next_number(&mut self) -> Self::TNumber

Returns the next random number.

Provided Methods§

Source

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

Fills the given mut slice with random bytes.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<ANumber: Number> RandomnessSource for ConstantRandomnessSource<ANumber>

Source§

type TNumber = ANumber

Source§

impl<ANumber: Number> RandomnessSource for OsRandomnessSource<ANumber>

Source§

type TNumber = ANumber