pub trait TurboCore {
// Required method
fn fill_bytes(&self, buffer: &mut [u8]);
}
Expand description
Base trait for implementing a PRNG. Only one method must be
implemented: TurboCore::fill_bytes
, which provides the basis
for any PRNG, to fill a buffer of bytes with random data.
This trait is object-safe.
§General Notes
When implementing on top of TurboCore
, the following considerations
should be made:
Default
- should be implemented forstd
platforms, but defaults should be non-deterministic. It should initialise with a randomised seed as a default, with the intent being quick and simple but random number generation.core::fmt::Debug
- should be implemented, but with care so to not leak the internal state of the PRNG.PartialEq
- should be implemented along withEq
, so that easy comparisons can be made with PRNGs to see if they are in the same or different internal state.Clone
- should be implemented, but with deterministically derived new internal states for the cloned instances. The cloned instance should not equal the original, but given a set seed on the original, the cloned instance should derive a new state in a deterministic fashion.Copy
- Do not implementCopy
, as it makes it too implicit when handling references and passing around the instance. When a copy is made, this modifies the state of the original in producing the new state of the copied instance, which is not something you want to happen implicitly.
Required Methods§
fn fill_bytes(&self, buffer: &mut [u8])
fn fill_bytes(&self, buffer: &mut [u8])
Fills a mutable buffer with random bytes.
§Example
use turborand::prelude::*;
let rand = Rng::with_seed(Default::default());
let mut bytes = [0u8; 10];
rand.fill_bytes(&mut bytes);
assert_ne!(&bytes, &[0u8; 10], "output should not match a zeroed array");