Trait rand::BlockRngCore [] [src]

pub trait BlockRngCore {
    type Item;
    type Results: Default + AsRef<[Self::Item]>;
    fn generate(&mut self, results: &mut Self::Results);
}

A trait for RNGs which do not generate random numbers individually, but in blocks (typically [u32; N]). This technique is commonly used by cryptographic RNGs to improve performance.

Usage of this trait is optional, but provides two advantages: implementations only need to concern themselves with generation of the block, not the various RngCore methods (especially fill_bytes, where the optimal implementations are not trivial), and this allows ReseedingRng to perform periodic reseeding with very low overhead.

Example

use rand_core::BlockRngCore;
use rand_core::impls::BlockRng;
 
struct MyRngCore;
 
impl BlockRngCore for MyRngCore {
    type Results = [u32; 16];
     
    fn generate(&mut self, results: &mut Self::Results) {
        unimplemented!()
    }
}
 
impl SeedableRng for MyRngCore {
    type Seed = unimplemented!();
    fn from_seed(seed: Self::Seed) -> Self {
        unimplemented!()
    }
}
 
// optionally, also implement CryptoRng for MyRngCore
 
// Final RNG.
type MyRng = BlockRng<u32, MyRngCore>;

Associated Types

Results element type, e.g. u32.

Results type. This is the 'block' an RNG implementing BlockRngCore generates, which will usually be an array like [u32; 16].

Required Methods

Generate a new block of results.

Implementors