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

Platform-independent geometric distribution. More...

#include <PRNGDistribution.hpp>

Classes

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

Public Types

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

Public Member Functions

 GeometricDistribution ()
 Default constructor. Creates geometric distribution with p=0.5.
 
 GeometricDistribution (double p)
 Constructs a geometric distribution with specified success probability.
 
 GeometricDistribution (const param_type &param)
 Constructs from a parameter set.
 
void reset ()
 Resets the distribution state.
 
double p () const
 Gets the success probability.
 
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 (0).
 
result_type max () const
 Gets the theoretical maximum value (unbounded).
 
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 GeometricDistribution &lhs, const GeometricDistribution &rhs)
 
bool operator!= (const GeometricDistribution &lhs, const GeometricDistribution &rhs)
 

Detailed Description

template<typename IntType = int>
class Infinity::Engine::GeometricDistribution< IntType >

Platform-independent geometric distribution.

Produces integer counts representing the number of failures before the first success in a sequence of independent Bernoulli trials with success probability p. This is the discrete analog of the exponential distribution.

The geometric distribution models waiting time scenarios like the number of attempts before success, or time until first event. It's the only discrete distribution with the memoryless property, making it ideal for modeling random "first occurrence" events.

Template Parameters
IntTypeInteger type for the result

Example usage:

PRNG rng(12345);
// Number of attempts before first success (20% success rate)
int failures_before_success = attempts(rng);
// Simulate finding a rare item (5% drop rate)
GeometricDistribution<int> item_drops(0.05);
int attempts_needed = item_drops(rng);
// Model enemy spawn failures before success
GeometricDistribution<int> spawn_tries(0.1);
int failed_spawns = spawn_tries(rng);
// Critical hit attempts in combat
GeometricDistribution<int> crit_attempts(0.15); // 15% crit rate
int attacks_until_crit = crit_attempts(rng);
Platform-independent geometric distribution.
Definition PRNGDistribution.hpp:2905
Pseudo-random number generator for procedural generation.
Definition PRNG.hpp:48
Note
Returns values in [0, ∞) representing failures before first success.
Memoryless property: P(X = n+k | X >= n) = P(X = k).
Mean = (1-p)/p, Variance = (1-p)/p².
For p = 0.5, expected value is 1 (one failure before success on average).

Member Typedef Documentation

◆ result_type

template<typename IntType = int>
using Infinity::Engine::GeometricDistribution< IntType >::result_type = IntType

The type of values produced by the distribution.

Constructor & Destructor Documentation

◆ GeometricDistribution() [1/3]

template<typename IntType = int>
Infinity::Engine::GeometricDistribution< IntType >::GeometricDistribution ( )
inline

Default constructor. Creates geometric distribution with p=0.5.

◆ GeometricDistribution() [2/3]

template<typename IntType = int>
Infinity::Engine::GeometricDistribution< IntType >::GeometricDistribution ( double  p)
inlineexplicit

Constructs a geometric distribution with specified success probability.

Parameters
pProbability of success

◆ GeometricDistribution() [3/3]

template<typename IntType = int>
Infinity::Engine::GeometricDistribution< IntType >::GeometricDistribution ( const param_type param)
inlineexplicit

Constructs from a parameter set.

Parameters
paramThe distribution parameters

Member Function Documentation

◆ max()

template<typename IntType = int>
result_type Infinity::Engine::GeometricDistribution< IntType >::max ( ) const
inline

Gets the theoretical maximum value (unbounded).

◆ min()

template<typename IntType = int>
result_type Infinity::Engine::GeometricDistribution< IntType >::min ( ) const
inline

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

◆ operator()() [1/2]

template<typename IntType = int>
template<typename Generator >
result_type Infinity::Engine::GeometricDistribution< IntType >::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 random count of failures before first success

◆ operator()() [2/2]

template<typename IntType = int>
template<typename Generator >
result_type Infinity::Engine::GeometricDistribution< IntType >::operator() ( Generator &  g,
const param_type param 
)
inline

Generates the next random value using provided parameters.

Uses the inverse CDF method with logarithms: X = ceil(ln(U) / ln(1-p)) - 1 where U ~ Uniform(0, 1) and X ~ Geometric(p).

This method is efficient and exact, requiring only one uniform sample and two logarithm computations.

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 random count of failures before first success

◆ p()

template<typename IntType = int>
double Infinity::Engine::GeometricDistribution< IntType >::p ( ) const
inline

Gets the success probability.

◆ param() [1/2]

template<typename IntType = int>
param_type Infinity::Engine::GeometricDistribution< IntType >::param ( ) const
inline

Gets the current parameter set.

◆ param() [2/2]

template<typename IntType = int>
void Infinity::Engine::GeometricDistribution< IntType >::param ( const param_type param)
inline

Sets new parameters for the distribution.

◆ reset()

template<typename IntType = int>
void Infinity::Engine::GeometricDistribution< IntType >::reset ( )
inline

Resets the distribution state.

No-op for geometric distribution as it maintains no internal state.

Friends And Related Symbol Documentation

◆ operator!=

template<typename IntType = int>
bool operator!= ( const GeometricDistribution< IntType > &  lhs,
const GeometricDistribution< IntType > &  rhs 
)
friend

◆ operator==

template<typename IntType = int>
bool operator== ( const GeometricDistribution< IntType > &  lhs,
const GeometricDistribution< IntType > &  rhs 
)
friend