Ocean
Ocean::RandomGenerator Class Reference

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...
 
RandomGeneratoroperator= (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...
 

Detailed Description

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)
{
/// create a local random generator object which uses the function's random generator to create a new seed value
RandomGenerator localRandomGenerator(randomGenerator);
for (unsigned int n = firstObject; n <= firstObject + numberObjects; ++n)
{
/// we create random values and use the local random generator (not the function's random generator)
data[n] = RandomI::random(localRandomGenerator, 100u);
}
}
RandomGenerator()
Creates a new random generator and initializes the internal parameter with the random value of the st...
static unsigned int random(const unsigned int maxValue)
Returns one random integer value with specified maximum value.
See also
RandomI, Worker.

Constructor & Destructor Documentation

◆ 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]

Ocean::RandomGenerator::RandomGenerator ( RandomGenerator generator)
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.

Parameters
generatorRandom 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
optionalGeneratorThe 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
randomGeneratorThe 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
seedThe seed initialization value, with range [0, infinity)

Member Function Documentation

◆ 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=()

RandomGenerator& Ocean::RandomGenerator::operator= ( RandomGenerator &&  randomGenerator)

Move operator.

Parameters
randomGeneratorThe 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

Field Documentation

◆ 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

Generator lock.

◆ 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: