Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Public Member Functions | List of all members
gf::Random Class Reference

A random engine. More...

#include <gf/Random.h>

Public Member Functions

 Random ()
 Default constructor with complex initialization. More...
 
 Random (std::uint_fast32_t seed)
 Constructor with simple initialization. More...
 
template<typename T >
T computeUniformInteger (T min, T max)
 Compute an integer with a uniform distribution. More...
 
template<typename T >
T computeUniformFloat (T min, T max)
 Compute a float with a uniform distribution. More...
 
template<typename T >
T computeNormalFloat (T mean, T stddev)
 Compute a float with a normal (Gaussian) distribution. More...
 
bool computeBernoulli (double p)
 Compute a boolean with a Bernoulli distribution. More...
 
std::mt19937 & getEngine ()
 Get the underlying engine. More...
 

Detailed Description

A random engine.

gf::Random is a wrapper around C++11 standard random features. It embeds a Mersenne Twister engine and provides several distributions above this engine.

Constructor & Destructor Documentation

gf::Random::Random ( )

Default constructor with complex initialization.

This constructor initializes the Mersenne Twister thanks to a random device and a seed sequence. This ensures that the state of the engine is different for each instanciation. Choose this constructor if you need good statistical randomness.

gf::Random::Random ( std::uint_fast32_t  seed)
inline

Constructor with simple initialization.

This constructor initializes the Mersenne Twister with a single seed. This method is easy but not very good because it allows only \( 2^{32} \) possible states. Choose this constructor if you need reproducible randomness as a same seed will always provide the same sequence.

Parameters
seedThe seed for the engine

Member Function Documentation

bool gf::Random::computeBernoulli ( double  p)
inline

Compute a boolean with a Bernoulli distribution.

Parameters
pThe probability of true
Returns
true with a probability of \( p \).
template<typename T >
T gf::Random::computeNormalFloat ( T  mean,
T  stddev 
)
inline

Compute a float with a normal (Gaussian) distribution.

Parameters
meanThe mean of the distribution
stddevThe standard deviation of the distribution
Returns
A value with a normal distribution
template<typename T >
T gf::Random::computeUniformFloat ( T  min,
T  max 
)
inline

Compute a float with a uniform distribution.

Parameters
minThe minimum value (inclusive)
maxThe maximum value (exclusive)
Returns
A value between min and max
template<typename T >
T gf::Random::computeUniformInteger ( T  min,
T  max 
)
inline

Compute an integer with a uniform distribution.

Parameters
minThe minimum value (inclusive)
maxThe maximum value (inclusive)
Returns
A value between min and max
std::mt19937& gf::Random::getEngine ( )
inline

Get the underlying engine.

Returns
A reference to the engine