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
-
| RealType | Floating point type (float or double) |
Example usage:
float time = waiting_time(rng);
float interval = exponential(rng);
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).
template<typename RealType = double>
template<typename Generator >
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:
- Set d = alpha - 1/3, c = 1/sqrt(9d)
- Generate Z ~ Normal(0,1), let V = (1 + cZ)³
- Generate U ~ Uniform(0,1)
- Use squeeze and log tests for acceptance
- If accepted, return dV * beta
- Template Parameters
-
| Generator | The random number generator type (e.g., PRNG) |
- Parameters
-
| g | The random number generator |
| param | The distribution parameters to use for this generation |
- Returns
- A positive random value from Gamma(param.alpha(), param.beta())