Blaze  3.6
Classes
Random number generation

Classes

class  blaze::Random< Type >
 Random number generator.The Random class encapsulates the initialization of the given random number generator with a pseudo-random seed obtained by the std::time() function. Currently, the mersenne-twister mt19937 as provided by the C++ standard library is used per default. For more information see the for instance the following documentation of the random number functionality of the C++11 standard library: More...
 
class  blaze::Rand< T >
 Default implementation of the Rand class for integral data types.This default implementation of the Rand class creates random, integral numbers in the range $ [0..max] $, where max is the maximal value of the given data type T. More...
 

Random number functions

template<typename T >
blaze::rand ()
 Random number function. More...
 
template<typename T , typename... Args>
blaze::rand (Args &&... args)
 Random number function. More...
 
template<typename T >
void blaze::randomize (T &&value)
 Randomization of a given variable. More...
 
template<typename T , typename... Args>
void blaze::randomize (T &&value, Args &&... args)
 Randomization of a given variable. More...
 
uint32_t blaze::defaultSeed ()
 Returns the default random seed. More...
 
uint32_t blaze::getSeed ()
 Returns the current seed of the random number generator. More...
 
void blaze::setSeed (uint32_t seed)
 Setting the seed of the random number generator. More...
 

Detailed Description

The random number module provides the functionality to generate pseudo-random numbers within the Blaze library. In order to create series of random numbers, the following functions are are provided:

The templated rand() function is capable of generating random numbers for built-in integer and floating point data types as well as complex values. The rand() function can be given up to five parameters that depending on the type of the random number may have different meaning. The following example demonstrates the random number generation:

using namespace blaze;
// The setSeed function sets the seed for the random number generator. This function can
// be used to set a specific seed for the random number generation, e.g. in order to
// reproduce an exact series of random numbers. If the setSeed function is not used, the
// random number generation uses a random seed.
setSeed( 12345 );
// In order to acquire the currently used seed, the getSeed() function can be used.
uint32_t seed = getSeed();
// Generating random numbers of built-in type. In case no range is provided, random numbers
// of integral type are generated in the range [0..max], where max is the largest possible
// value of the specified type and random floating point numbers are generated in the range
// [0..1). In the example, the random double precision floating point value is created in
// the range [2..4].
int i = rand<int>();
double d = rand<double>( 2.0, 4.0 );
// Generating random complex numbers. In case no range is provided, both the real and the
// imaginary part are created depending on the element type of the complex number. In case
// one range is specified, both the real and the imaginary part are created within the
// specified range. In the example, both parts are created within the range [1..4]. If two
// ranges are provided, the real part is created in the first range and the imaginary part
// is created in the second range. The last example demonstrates this by restricting the
// real part to the range [2..3] and the imaginary part to the range [1..5].
complex<float> c1 = rand< complex<float> >();
complex<float> c2 = rand< complex<float> >( 1.0F, 4.0F );
complex<float> c3 = rand< complex<float> >( 2.0F, 3.0F, 1.0F, 5.0F );
Note
In order to reproduce certain series of random numbers, the seed of the random number generator has to be set explicitly via the setSeed() function. Otherwise a random seed is used for the random number generation.

Function Documentation

◆ defaultSeed()

uint32_t blaze::defaultSeed ( )
inline

Returns the default random seed.

Returns
The default random seed.

◆ getSeed()

uint32_t blaze::getSeed ( )
inline

Returns the current seed of the random number generator.

Returns
The current seed of the random number generator.

◆ rand() [1/2]

template<typename T >
T blaze::rand ( )
inline

Random number function.

Returns
The generated random number.

The rand() function returns a default random number depending on the given data type. In case of integral data types, the function returns a random number in the range $ [0..max] $, where max is the maximal value of the data type T. In case of floating point data types, the function returns a random number in the range $ [0..1) $. In case of complex data types, the function returns a random complex value where both the real and the imaginary part have been set according to the element type of the complex value ( $ [0..max] $ for integral elements, $ [0..1) $ for floating point elements).

◆ rand() [2/2]

template<typename T , typename... Args>
T blaze::rand ( Args &&...  args)
inline

Random number function.

Parameters
argsThe arguments for the random number generation.
Returns
The generated random number.

This rand() function creates a random number based on the given arguments args.

◆ randomize() [1/2]

template<typename T >
void blaze::randomize ( T &&  value)
inline

Randomization of a given variable.

Parameters
valueThe variable to be randomized.
Returns
void

The randomize() function randomizes the given variable depending on its data type. In case of integral data types, the function returns a random number in the range $ [0..max] $, where max is the maximal value of the data type T. In case of floating point data types, the function returns a random number in the range $ [0..1) $. In case of complex data types, the function returns a random complex value where both the real and the imaginary part have been set according to the element type of the complex value ( $ [0..max] $ for integral elements, $ [0..1) $ for floating point elements).

◆ randomize() [2/2]

template<typename T , typename... Args>
void blaze::randomize ( T &&  value,
Args &&...  args 
)
inline

Randomization of a given variable.

Parameters
valueThe value to be randomized.
argsThe arguments for the random number generation.
Returns
void

This randomize() function randomizes the given variable based on the given arguments args.

◆ setSeed()

void blaze::setSeed ( uint32_t  seed)
inline

Setting the seed of the random number generator.

Parameters
seedThe new seed for the random number generator.
Returns
void

This function can be used to set the seed for the random number generation in order to create a reproducible series of random numbers.