Pigweed
 
Loading...
Searching...
No Matches
pw::random::XorShiftStarRng64 Class Reference

#include <xor_shift.h>

Inheritance diagram for pw::random::XorShiftStarRng64:
pw::random::RandomGenerator

Public Member Functions

 XorShiftStarRng64 (uint64_t initial_seed)
 
void Get (ByteSpan dest) final
 
void InjectEntropyBits (uint32_t data, uint_fast8_t num_bits) final
 
- Public Member Functions inherited from pw::random::RandomGenerator
template<class T >
void GetInt (T &dest)
 
template<class T >
void GetInt (T &dest, const T &exclusive_upper_bound)
 
virtual void Get (ByteSpan dest)=0
 
virtual void InjectEntropyBits (uint32_t data, uint_fast8_t num_bits)=0
 
void InjectEntropy (ConstByteSpan data)
 Injects entropy into the pool byte-by-byte.
 

Detailed Description

A random generator based off the xorshift* algorithm.

The state is represented as an integer that, with each generation, performs exclusive OR (XOR) operations on different left/right bit shifts of itself. The * in xorshift* refers to a final multiplication that is applied to the output value. The final multiplication is essentially a nonlinear transformation that makes the algorithm stronger than a plain XOR shift.

Pigweed's implementation augments xorshift* with an ability to inject entropy to reseed the generator throughout its lifetime. When entropy is injected, the results of the generator are no longer completely deterministic based on the original seed.

See also Xorshift RNGs and An experimental exploration of Marsaglia's xorshift generators, scrambled.

Warning
This random generator is NOT cryptographically secure. It incorporates pseudo-random generation to extrapolate any true injected entropy. The distribution is not guaranteed to be uniform.

Member Function Documentation

◆ Get()

void pw::random::XorShiftStarRng64::Get ( ByteSpan  dest)
inlinefinalvirtual

Populates the destination buffer with a randomly generated value.

This generator uses entropy-seeded PRNG to never exhaust its random number pool.

Implements pw::random::RandomGenerator.

◆ InjectEntropyBits()

void pw::random::XorShiftStarRng64::InjectEntropyBits ( uint32_t  data,
uint_fast8_t  num_bits 
)
inlinefinalvirtual

Injects entropy by rotating the state by the number of entropy bits before XORing the entropy with the current state.

This technique ensures that seeding the random value with single bits will progressively fill the state with more entropy.

Implements pw::random::RandomGenerator.


The documentation for this class was generated from the following file: