FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
FEAT::Random Class Reference

Pseudo-Random Number Generator. More...

#include <random.hpp>

Public Types

typedef std::uint64_t SeedType
 seed type More...
 

Public Member Functions

 Random ()
 Default constructor that seeds the RNG with the current high-resolution clock time. More...
 
 Random (SeedType seed)
 Constructor. More...
 
SeedType get_seed () const
 
uint64_t next ()
 Returns the next number in the stream. More...
 
template<typename T_ >
T_ operator() (T_ a, T_ b)
 Ranged evaluation operator. More...
 
template<typename T_ >
Randomoperator>> (T_ &x)
 Extraction operator. More...
 

Static Public Attributes

static constexpr SeedType xor_mult = SeedType(2685821657736338717ull)
 internal multiplier More...
 

Private Attributes

std::uint64_t _seed
 the rng's original seed More...
 
std::uint64_t _x
 the rng's working values More...
 

Detailed Description

Pseudo-Random Number Generator.

This class implements a 64-bit XOR-Shift* pseudo-random number generator with a 64-bit cycle.

The intention of this class is to provide a pseudo-random number generator which generates (warning: paradox ahead) a reproducible random number stream independent of the platform and the compiler in use.

Warning
This class is (intentionally) not thread-safe – in a multi-threaded application each thread should use its own random number generator.
See also
S. Vigna: An experimental exploration of Marsaglia's xorshift generators, scrambled; Cornell University Library, arXiv:1402.6246
http://arxiv.org/abs/1402.6246
G. Marsaglia: Xorshift RNGs; Journal of Statistical Software, Vol. 8 (Issue 14), 2003
http://www.jstatsoft.org/v08/i14
http://en.wikipedia.org/wiki/Xorshift
Note
The implementation in this class corresponds to the xorshift64* example in Figure 10 of the first article by Vigna mentioned above.
Author
Peter Zajac

Definition at line 53 of file random.hpp.

Member Typedef Documentation

◆ SeedType

typedef std::uint64_t FEAT::Random::SeedType

seed type

Definition at line 57 of file random.hpp.

Constructor & Destructor Documentation

◆ Random() [1/2]

FEAT::Random::Random ( )
inlineexplicit

Default constructor that seeds the RNG with the current high-resolution clock time.

Definition at line 72 of file random.hpp.

References _seed, and _x.

◆ Random() [2/2]

FEAT::Random::Random ( SeedType  seed)
inlineexplicit

Constructor.

Parameters
[in]seedThe seed for the random number generator; must not be 0.

Definition at line 87 of file random.hpp.

References XASSERTM.

Member Function Documentation

◆ get_seed()

SeedType FEAT::Random::get_seed ( ) const
inline
Returns
The original seed that was used to create this RNG object.

Definition at line 95 of file random.hpp.

References _seed.

◆ next()

uint64_t FEAT::Random::next ( )
inline

Returns the next number in the stream.

This function returns the next unsigned 32-bit integer in the random number stream and advances the stream.

Definition at line 106 of file random.hpp.

References _x, and xor_mult.

◆ operator()()

template<typename T_ >
T_ FEAT::Random::operator() ( T_  a,
T_  b 
)
inline

Ranged evaluation operator.

This operator returns a random number in the range passed to this operator.

Template Parameters
T_The type of the value to be extracted. Is determined automatically by a and b.
Parameters
[in]a,bThe minimum and maximum values for the range of the random number to be generated.
Note
If b < a, then a and b are swapped.
If a = b, then the returned value will be equal to a, however, the generator will still advance its position within the random number stream.
Returns
A random value in the range [a, b].

Definition at line 157 of file random.hpp.

◆ operator>>()

template<typename T_ >
Random & FEAT::Random::operator>> ( T_ &  x)
inline

Extraction operator.

This function extracts the next random number in the stream.

  • If the type of the value to be extracted is integral, then the value will be within the full range, i.e.
    • for a signed n-bit integer, it will hold -2^(n-1) <= x < 2^(n-1)
    • for an unsigned n-bit integer, it will hold that 0 <= x < 2^n
  • If the type of the value to be extracted is a floating point type, then the value will be within the closed interval [0,1].
Template Parameters
T_The type of the value to be extracted. Must be either of integral or floating point type.
Parameters
[out]xThe transient reference to an object receiving the extracted value.
Returns
*this

Definition at line 133 of file random.hpp.

Member Data Documentation

◆ _seed

std::uint64_t FEAT::Random::_seed
private

the rng's original seed

Definition at line 64 of file random.hpp.

Referenced by Random(), and get_seed().

◆ _x

std::uint64_t FEAT::Random::_x
private

the rng's working values

Definition at line 66 of file random.hpp.

Referenced by Random(), and next().

◆ xor_mult

constexpr SeedType FEAT::Random::xor_mult = SeedType(2685821657736338717ull)
staticconstexpr

internal multiplier

Definition at line 60 of file random.hpp.

Referenced by next().


The documentation for this class was generated from the following file: