Gamedev Framework (gf)  0.11.0
A C++14 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...
 
Vector2f computePosition (const RectF &area)
 Compute a uniform position in a given area. More...
 
Vector2i computePosition (const RectI &area)
 Compute a uniform position in a given area. 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

◆ Random() [1/2]

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.

◆ Random() [2/2]

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

◆ computeBernoulli()

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 \).

◆ computeNormalFloat()

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

◆ computePosition() [1/2]

Vector2f gf::Random::computePosition ( const RectF area)

Compute a uniform position in a given area.

Parameters
areaThe area
Returns
A value in the area

◆ computePosition() [2/2]

Vector2i gf::Random::computePosition ( const RectI area)

Compute a uniform position in a given area.

Parameters
areaThe area
Returns
A value in the area

◆ computeUniformFloat()

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

◆ computeUniformInteger()

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

◆ getEngine()

std::mt19937& gf::Random::getEngine ( )
inline

Get the underlying engine.

Returns
A reference to the engine