This class implements a generator for random numbers.
More...
#include <RandomGenerator.h>
|
| RandomGenerator () |
| Creates a new random generator and initializes the internal parameter with the random value of the standard random function.
|
|
| 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.
|
|
| 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.
|
|
| RandomGenerator (RandomGenerator &&randomGenerator) |
| Move constructor.
|
|
| RandomGenerator (const unsigned int seed) |
| Creates a new random generator and initializes the internal parameter by the given value.
|
|
unsigned int | rand () |
| Returns the next random number.
|
|
unsigned int | lockedRand () |
| Returns the next random number.
|
|
unsigned int | seed () const |
| Returns the current seed value of this object.
|
|
unsigned int | initialSeed () const |
| Returns the initial seed value which was used to initialize this random generator.
|
|
RandomGenerator & | operator= (RandomGenerator &&randomGenerator) |
| Move operator.
|
|
|
static constexpr unsigned int | randMax () |
| Returns the maximal random value of this generator.
|
|
|
unsigned int | initialSeed_ = (unsigned int)(-1) |
| The seed value which was used to initialize this random generator.
|
|
unsigned int | seed_ = (unsigned int)(-1) |
| Internal seed parameter used for random number generation, changes whenever a new random number is generated.
|
|
Lock | lock_ |
| Generator lock.
|
|
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):
void multiCoreFunction(
RandomGenerator& randomGenerator,
unsigned int* data,
unsigned int firstObject,
unsigned int numberObjects)
{
for (unsigned int n = firstObject; n <= firstObject + numberObjects; ++n)
{
}
}
This class implements a generator for random numbers.
Definition RandomGenerator.h:42
static unsigned int random(const unsigned int maxValue)
Returns one random integer value with specified maximum value.
- See also
- RandomI, Worker.
◆ RandomGenerator() [1/5]
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.
- See also
- RandomI::initialize().
◆ RandomGenerator() [2/5]
Creates a new random generator and initializes the internal parameter by a random value provided by the locked random function of the given generator.
- Parameters
-
generator | Random number generator used for initialization, the generator's seed will be changed during the initialization |
◆ RandomGenerator() [3/5]
Ocean::RandomGenerator::RandomGenerator |
( |
RandomGenerator * |
optionalGenerator | ) |
|
|
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.
- Parameters
-
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 |
◆ RandomGenerator() [4/5]
Ocean::RandomGenerator::RandomGenerator |
( |
RandomGenerator && |
randomGenerator | ) |
|
|
inline |
Move constructor.
- Parameters
-
randomGenerator | The random generator object to be moved |
◆ RandomGenerator() [5/5]
Ocean::RandomGenerator::RandomGenerator |
( |
const unsigned int |
seed | ) |
|
|
inlineexplicit |
Creates a new random generator and initializes the internal parameter by the given value.
- Parameters
-
seed | The seed initialization value, with range [0, infinity) |
◆ initialSeed()
unsigned int Ocean::RandomGenerator::initialSeed |
( |
| ) |
const |
|
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.
- Returns
- The random generator's initial seed value
◆ lockedRand()
unsigned int Ocean::RandomGenerator::lockedRand |
( |
| ) |
|
|
inline |
Returns the next random number.
This function is thread safe.
- Returns
- Random number with range [0, 32767]
- See also
- rand().
◆ operator=()
Move operator.
- Parameters
-
randomGenerator | The random generator object to be moved |
- Returns
- Reference to this object
◆ rand()
unsigned int Ocean::RandomGenerator::rand |
( |
| ) |
|
|
inline |
Returns the next random number.
Beware: This function is not thread safe.
- Returns
- Random number with range [0, 32767]
- See also
- lockedRand().
◆ randMax()
constexpr unsigned int Ocean::RandomGenerator::randMax |
( |
| ) |
|
|
staticconstexpr |
Returns the maximal random value of this generator.
- Returns
- Maximal random value of this generator
◆ seed()
unsigned int Ocean::RandomGenerator::seed |
( |
| ) |
const |
|
inline |
Returns the current seed value of this object.
This seed value changes whenever a new random number is generated.
- Returns
- Current seed value
◆ threadAndTimeBasedSeed()
static unsigned int Ocean::RandomGenerator::threadAndTimeBasedSeed |
( |
| ) |
|
|
staticprivate |
Returns a seed value based on the current time, the thread id, and a random value from RandomI.
- Returns
- The combined seed value
◆ initialSeed_
unsigned int Ocean::RandomGenerator::initialSeed_ = (unsigned int)(-1) |
|
private |
The seed value which was used to initialize this random generator.
◆ lock_
Lock Ocean::RandomGenerator::lock_ |
|
private |
◆ seed_
unsigned int Ocean::RandomGenerator::seed_ = (unsigned int)(-1) |
|
private |
Internal seed parameter used for random number generation, changes whenever a new random number is generated.
The documentation for this class was generated from the following file: