libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
Random Class Reference

Pseudo-random number generator wrapping std::mt19937_64. More...

#include <random.h>

Public Member Functions

 Random ()
 Constructs a Random seeded from std::random_device.
 
 Random (uint64_t seed)
 Constructs a Random with a specific seed.
 
void seed (uint64_t seed)
 Reseeds the engine.
 
int randomInt (int min, int max)
 Returns a uniformly distributed random integer in [min, max].
 
int64_t randomInt64 (int64_t min, int64_t max)
 Returns a uniformly distributed random 64-bit integer in [min, max].
 
double randomDouble (double min, double max)
 Returns a uniformly distributed random double in [min, max).
 
float randomFloat (float min, float max)
 Returns a uniformly distributed random float in [min, max).
 
Buffer randomBytes (size_t count)
 Returns a Buffer filled with random bytes.
 
bool randomBool ()
 Returns a random boolean with 50/50 probability.
 

Static Public Member Functions

static Error trueRandom (uint8_t *buf, size_t bytes)
 Fills a buffer with true random bytes from the OS entropy source.
 
static Randomglobal ()
 Returns a thread-local global Random instance.
 

Detailed Description

Pseudo-random number generator wrapping std::mt19937_64.

Random provides two distinct categories of random data:

Pseudo-random (PRNG): All instance methods use a deterministic Mersenne Twister engine (std::mt19937_64). Given the same seed, the sequence is perfectly reproducible — useful for simulations, testing, and any case where repeatability matters. The default constructor seeds from std::random_device, so unseeded instances produce non-reproducible sequences that are statistically uniform but not cryptographically secure. A thread-local global instance is available via global() for convenience.

True random: The static trueRandom() method reads directly from the platform's hardware/OS entropy source (std::random_device) on every call. This is suitable for cryptographic seeds, UUIDs, nonces, and key generation. It is slower than PRNG output and should not be used for bulk data generation.

Constructor & Destructor Documentation

◆ Random()

Random::Random ( uint64_t  seed)
explicit

Constructs a Random with a specific seed.

Parameters
seedThe seed value for the engine.

Member Function Documentation

◆ global()

static Random & Random::global ( )
static

Returns a thread-local global Random instance.

Returns
Reference to the thread-local instance.

◆ randomBool()

bool Random::randomBool ( )

Returns a random boolean with 50/50 probability.

Returns
true or false with equal probability.

◆ randomBytes()

Buffer Random::randomBytes ( size_t  count)

Returns a Buffer filled with random bytes.

Parameters
countNumber of random bytes.
Returns
A Buffer of the given size filled with random data.

◆ randomDouble()

double Random::randomDouble ( double  min,
double  max 
)

Returns a uniformly distributed random double in [min, max).

Parameters
minLower bound (inclusive).
maxUpper bound (exclusive).
Returns
A random double.

◆ randomFloat()

float Random::randomFloat ( float  min,
float  max 
)

Returns a uniformly distributed random float in [min, max).

Parameters
minLower bound (inclusive).
maxUpper bound (exclusive).
Returns
A random float.

◆ randomInt()

int Random::randomInt ( int  min,
int  max 
)

Returns a uniformly distributed random integer in [min, max].

Parameters
minLower bound (inclusive).
maxUpper bound (inclusive).
Returns
A random integer.

◆ randomInt64()

int64_t Random::randomInt64 ( int64_t  min,
int64_t  max 
)

Returns a uniformly distributed random 64-bit integer in [min, max].

Parameters
minLower bound (inclusive).
maxUpper bound (inclusive).
Returns
A random 64-bit integer.

◆ seed()

void Random::seed ( uint64_t  seed)

Reseeds the engine.

Parameters
seedThe new seed value.

◆ trueRandom()

static Error Random::trueRandom ( uint8_t buf,
size_t  bytes 
)
static

Fills a buffer with true random bytes from the OS entropy source.

Reads directly from std::random_device on every call. Unlike the instance methods, this does not use a deterministic PRNG — each byte is drawn from the platform's entropy pool. Suitable for cryptographic seeding, UUID generation, and nonce creation.

Parameters
bufPointer to the destination buffer.
bytesNumber of bytes to generate.
Returns
Error::Ok on success.

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