Ocean
|
This class implements a generator for random numbers. More...
Public Member Functions | |
RandomGenerator () | |
Creates a new random generator and initializes the internal parameter with the random value of the standard random function. More... | |
RandomGenerator (RandomGenerator &generator) | |
Creates a new random generator and initializes the internal parameter by a random value provided by the locked random function of the given generator. More... | |
RandomGenerator (RandomGenerator *optionalGenerator) | |
Creates a new random generator and optional initializes the internal parameter by a random value provided by the locked random function of the given generator, if no generator is provided the standard random function will be used for initialization. More... | |
RandomGenerator (RandomGenerator &&randomGenerator) | |
Move constructor. More... | |
RandomGenerator (const unsigned int seed) | |
Creates a new random generator and initializes the internal parameter by the given value. More... | |
unsigned int | rand () |
Returns the next random number. More... | |
unsigned int | lockedRand () |
Returns the next random number. More... | |
unsigned int | seed () const |
Returns the current seed value of this object. More... | |
unsigned int | initialSeed () const |
Returns the initial seed value which was used to initialize this random generator. More... | |
RandomGenerator & | operator= (RandomGenerator &&randomGenerator) |
Move operator. More... | |
Static Public Member Functions | |
static constexpr unsigned int | randMax () |
Returns the maximal random value of this generator. More... | |
Static Private Member Functions | |
static unsigned int | threadAndTimeBasedSeed () |
Returns a seed value based on the current time, the thread id, and a random value from RandomI. More... | |
Private Attributes | |
unsigned int | initialSeed_ = (unsigned int)(-1) |
The seed value which was used to initialize this random generator. More... | |
unsigned int | seed_ = (unsigned int)(-1) |
Internal seed parameter used for random number generation, changes whenever a new random number is generated. More... | |
Lock | lock_ |
Generator lock. More... | |
This class implements a generator for random numbers.
A random generator object can be used to improve the performance of code (needing random numbers) which is applied on several CPU cores in parallel.
The default random number functions of the standard library are thread-safe but may apply expensive locks to synchronize the individual seed parameters of the individual threads.
Therefore, this class can be used to improve the code performance significantly in multi-threaded environments.
The following code example shows the correct application of this class for multi-core functions (which can be invoked e.g., by the Worker class):
Ocean::RandomGenerator::RandomGenerator | ( | ) |
Creates a new random generator and initializes the internal parameter with the random value of the standard random function.
Ensure that RandomI::initialize() has been called before using this constructor.
|
inlineexplicit |
Creates a new random generator and initializes the internal parameter by a random value provided by the locked random function of the given generator.
generator | Random number generator used for initialization, the generator's seed will be changed during the initialization |
|
explicit |
Creates a new random generator and optional initializes the internal parameter by a random value provided by the locked random function of the given generator, if no generator is provided the standard random function will be used for initialization.
optionalGenerator | The random number generator used for initialization (the generator's seed will be changed during the initialization), nullptr to use the standard random function for initialization |
|
inline |
Move constructor.
randomGenerator | The random generator object to be moved |
|
inlineexplicit |
Creates a new random generator and initializes the internal parameter by the given value.
seed | The seed initialization value, with range [0, infinity) |
|
inline |
Returns the initial seed value which was used to initialize this random generator.
The initial seed value will not change during the lifetime of the generator.
|
inline |
Returns the next random number.
This function is thread safe.
RandomGenerator& Ocean::RandomGenerator::operator= | ( | RandomGenerator && | randomGenerator | ) |
Move operator.
randomGenerator | The random generator object to be moved |
|
inline |
Returns the next random number.
Beware: This function is not thread safe.
|
staticconstexpr |
Returns the maximal random value of this generator.
|
inline |
Returns the current seed value of this object.
This seed value changes whenever a new random number is generated.
|
staticprivate |
Returns a seed value based on the current time, the thread id, and a random value from RandomI.
|
private |
The seed value which was used to initialize this random generator.
|
private |
Generator lock.
|
private |
Internal seed parameter used for random number generation, changes whenever a new random number is generated.