Regina Calculation Engine
|
Offers threadsafe access to Regina's global uniform random bit generator. More...
#include <utilities/randutils.h>
Public Member Functions | |
RandomEngine () | |
Constructor that locks the internal mutex. More... | |
std::default_random_engine & | engine () |
Provides access to Regina's global uniform random bit generator (URBG). More... | |
Static Public Member Functions | |
template<typename Int > | |
static Int | rand (Int range) |
A convenience function that returns a random integer modulo range, in a thread-safe manner. More... | |
static void | reseedWithHardware () |
Reseeds the global uniform random bit generator using hardware entropy. More... | |
static void | reseedWithDefault () |
Reseeds the global uniform random bit generator using the default seed. More... | |
Offers threadsafe access to Regina's global uniform random bit generator.
Regina has at its heart a global uniform random bit generator (URBG) that complies with the C++ UniformRandomBitGenerator concept, and can be used with other data types and algorithms from the standard <random> header.
An object of this class offers threadsafe access to this global URBG.
This class behaves in many ways like std::lock_guard. On construction a RandomEngine object will lock an internal mutex that grants exclusive access to the global URBG, and on destruction the RandomEngine object will unlock this mutex.
The engine() function grants direct access to the URBG. To ensure thread safety, you should only use this URBG during the scope of the RandomEngine object (i.e., while the internal mutex is locked). The easiest way to do this is to never store a reference to engine() for later use, but instead to pass engine() directly to other random number generation functions as you use them.
The engine is seeded with a default value. This means that each time you run the same code in a new process (but on the same machine), you should receive the same sequence of random bits. However, the generation algorithm may be specific to your machine, so running the same code on different machines might well not generate the same random bits.
If you need to re-seed the random engine with a value that is unpredictable (e.g., using hardware entropy), you can call reseedWithHardware(). If you wish to re-seed the random engine with its default value (to behave as though the process had just started), you can call reseedWithDefault().