C/C++ API Reference
Loading...
Searching...
No Matches
pw_random

Oveview

Main docs: https://pigweed.dev/pw_random.

Classes

class  pw::random::RandomGenerator
 
class  pw::random::XorShiftStarRng64
 

Functions

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

Function Documentation

◆ Get() [1/2]

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.

◆ Get() [2/2]

virtual void pw::random::RandomGenerator::Get ( ByteSpan  dest)
pure virtual

Populates the destination buffer with a randomly generated value.

Parameters
[out]destThe destination buffer.

Implemented in pw::random::XorShiftStarRng64.

◆ GetInt()

template<class T >
void pw::random::RandomGenerator::GetInt ( T &  dest,
const T &  exclusive_upper_bound 
)
inline

Calculates a uniformly distributed random number in the range [0, exclusive_upper_bound).

This avoids modulo biasing. Uniformity is only guaranteed if the underlying generator generates uniform data. Uniformity is achieved by generating new random numbers until one is generated in the desired range (with optimizations).

Parameters
[out]destThe destination to populate the random number into.
[in]exclusive_upper_boundThe largest number that can be populated into dest, exclusive.

◆ InjectEntropyBits() [1/2]

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.

◆ InjectEntropyBits() [2/2]

virtual void pw::random::RandomGenerator::InjectEntropyBits ( uint32_t  data,
uint_fast8_t  num_bits 
)
pure virtual

Injects entropy into the pool.

Parameters
[in]dataUp to 32 bits of random entropy data.
[in]num_bitsThe number of bits of entropy. If less than 32, entropy is assumed to be stored in the least significant bits of data.

Implemented in pw::random::XorShiftStarRng64.