Infinity Engine v0.6.20
C++ API Documentation
Loading...
Searching...
No Matches
Infinity::Engine::GammaDistribution< RealType > Class Template Reference

Platform-independent gamma distribution. More...

#include <PRNGDistribution.hpp>

Inheritance diagram for Infinity::Engine::GammaDistribution< RealType >:
[legend]

Classes

struct  param_type
 Parameter set for the distribution. More...
 

Public Types

using result_type = RealType
 The type of values produced by the distribution.
 

Public Member Functions

 GammaDistribution ()
 Default constructor. Creates Gamma(1, 1) distribution.
 
 GammaDistribution (RealType alpha, RealType beta=RealType(1))
 Constructs a gamma distribution with specified parameters.
 
 GammaDistribution (const param_type &param)
 Constructs from a parameter set.
 
void reset ()
 Resets the distribution state.
 
RealType alpha () const
 Gets the shape parameter.
 
RealType beta () const
 Gets the scale parameter.
 
param_type param () const
 Gets the current parameter set.
 
void param (const param_type &param)
 Sets new parameters for the distribution.
 
result_type min () const
 Gets the minimum value that can be generated (approaches 0).
 
result_type max () const
 Gets the theoretical maximum value (positive infinity).
 
template<typename Generator >
result_type operator() (Generator &g)
 Generates the next random value using stored parameters.
 
template<typename Generator >
result_type operator() (Generator &g, const param_type &param)
 Generates the next random value using provided parameters.
 

Friends

bool operator== (const GammaDistribution &lhs, const GammaDistribution &rhs)
 
bool operator!= (const GammaDistribution &lhs, const GammaDistribution &rhs)
 

Detailed Description

template<typename RealType = double>
class Infinity::Engine::GammaDistribution< RealType >

Platform-independent gamma distribution.

Produces positive values according to the gamma distribution with shape parameter alpha and scale parameter beta. Uses Marsaglia & Tsang's method for alpha >= 1, which provides excellent efficiency through squeeze-rejection sampling.

The gamma distribution is versatile and appears in many contexts including waiting times, rainfall models, and as the basis for other distributions (exponential, chi-squared, etc.). It's characterized by two parameters: alpha (shape) determines the form of the distribution, while beta (scale) stretches or compresses it.

Template Parameters
RealTypeFloating point type (float or double)

Example usage:

PRNG rng(12345);
// Shape=2, scale=2 (often used for waiting times)
GammaDistribution<float> waiting_time(2.0f, 2.0f);
float time = waiting_time(rng);
// Exponential distribution is Gamma(1, beta)
GammaDistribution<float> exponential(1.0f, 1.5f);
float interval = exponential(rng);
// Model event durations with variability
GammaDistribution<double> duration(3.0, 1.5);
for (int i = 0; i < 100; ++i) {
double event_time = duration(rng);
}
Platform-independent gamma distribution.
Definition PRNGDistribution.hpp:1098
Pseudo-random number generator for procedural generation.
Definition PRNG.hpp:48
Note
All generated values are strictly positive.
Mean = alpha * beta, Variance = alpha * beta²
When alpha = 1, reduces to exponential distribution with rate 1/beta.
Chi-squared distribution with k degrees of freedom is Gamma(k/2, 2).

Member Typedef Documentation

◆ result_type

template<typename RealType = double>
using Infinity::Engine::GammaDistribution< RealType >::result_type = RealType

The type of values produced by the distribution.

Constructor & Destructor Documentation

◆ GammaDistribution() [1/3]

template<typename RealType = double>
Infinity::Engine::GammaDistribution< RealType >::GammaDistribution ( )
inline

Default constructor. Creates Gamma(1, 1) distribution.

◆ GammaDistribution() [2/3]

template<typename RealType = double>
Infinity::Engine::GammaDistribution< RealType >::GammaDistribution ( RealType  alpha,
RealType  beta = RealType(1) 
)
inlineexplicit

Constructs a gamma distribution with specified parameters.

Parameters
alphaShape parameter
betaScale parameter

◆ GammaDistribution() [3/3]

template<typename RealType = double>
Infinity::Engine::GammaDistribution< RealType >::GammaDistribution ( const param_type param)
inlineexplicit

Constructs from a parameter set.

Parameters
paramThe distribution parameters

Member Function Documentation

◆ alpha()

template<typename RealType = double>
RealType Infinity::Engine::GammaDistribution< RealType >::alpha ( ) const
inline

Gets the shape parameter.

◆ beta()

template<typename RealType = double>
RealType Infinity::Engine::GammaDistribution< RealType >::beta ( ) const
inline

Gets the scale parameter.

◆ max()

template<typename RealType = double>
result_type Infinity::Engine::GammaDistribution< RealType >::max ( ) const
inline

Gets the theoretical maximum value (positive infinity).

◆ min()

template<typename RealType = double>
result_type Infinity::Engine::GammaDistribution< RealType >::min ( ) const
inline

Gets the minimum value that can be generated (approaches 0).

◆ operator()() [1/2]

template<typename RealType = double>
template<typename Generator >
result_type Infinity::Engine::GammaDistribution< RealType >::operator() ( Generator &  g)
inline

Generates the next random value using stored parameters.

Template Parameters
GeneratorThe random number generator type (e.g., PRNG)
Parameters
gThe random number generator
Returns
A positive random value from the gamma distribution

◆ operator()() [2/2]

template<typename RealType = double>
template<typename Generator >
result_type Infinity::Engine::GammaDistribution< RealType >::operator() ( Generator &  g,
const param_type param 
)
inline

Generates the next random value using provided parameters.

Uses two different methods depending on alpha:

  • For alpha < 1: Uses a transformation method based on Gamma(alpha+1)
  • For alpha >= 1: Uses Marsaglia & Tsang's (2000) squeeze-rejection method

The Marsaglia & Tsang method is highly efficient, typically accepting on the first try with probability > 95%. It generates candidates from a transformed normal distribution and uses a squeeze test followed by a log test for acceptance/rejection.

Algorithm outline for alpha >= 1:

  1. Set d = alpha - 1/3, c = 1/sqrt(9d)
  2. Generate Z ~ Normal(0,1), let V = (1 + cZ)³
  3. Generate U ~ Uniform(0,1)
  4. Use squeeze and log tests for acceptance
  5. If accepted, return dV * beta
Template Parameters
GeneratorThe random number generator type (e.g., PRNG)
Parameters
gThe random number generator
paramThe distribution parameters to use for this generation
Returns
A positive random value from Gamma(param.alpha(), param.beta())

◆ param() [1/2]

template<typename RealType = double>
param_type Infinity::Engine::GammaDistribution< RealType >::param ( ) const
inline

Gets the current parameter set.

◆ param() [2/2]

template<typename RealType = double>
void Infinity::Engine::GammaDistribution< RealType >::param ( const param_type param)
inline

Sets new parameters for the distribution.

◆ reset()

template<typename RealType = double>
void Infinity::Engine::GammaDistribution< RealType >::reset ( )
inline

Resets the distribution state.

Resets the underlying normal distribution used by Marsaglia & Tsang's method.

Friends And Related Symbol Documentation

◆ operator!=

template<typename RealType = double>
bool operator!= ( const GammaDistribution< RealType > &  lhs,
const GammaDistribution< RealType > &  rhs 
)
friend

◆ operator==

template<typename RealType = double>
bool operator== ( const GammaDistribution< RealType > &  lhs,
const GammaDistribution< RealType > &  rhs 
)
friend