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 Random & | global () |
| Returns a thread-local global Random instance. | |
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.
|
explicit |
Constructs a Random with a specific seed.
| seed | The seed value for the engine. |
Returns a thread-local global Random instance.
| bool Random::randomBool | ( | ) |
Returns a random boolean with 50/50 probability.
Returns a uniformly distributed random double in [min, max).
| min | Lower bound (inclusive). |
| max | Upper bound (exclusive). |
Returns a uniformly distributed random float in [min, max).
| min | Lower bound (inclusive). |
| max | Upper bound (exclusive). |
| int Random::randomInt | ( | int | min, |
| int | max | ||
| ) |
Returns a uniformly distributed random integer in [min, max].
| min | Lower bound (inclusive). |
| max | Upper bound (inclusive). |
Returns a uniformly distributed random 64-bit integer in [min, max].
| min | Lower bound (inclusive). |
| max | Upper bound (inclusive). |
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.
| buf | Pointer to the destination buffer. |
| bytes | Number of bytes to generate. |