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

Platform-independent binomial 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

 BinomialDistribution ()
 Default constructor. Creates single trial distribution (t=1, p=0.5).
 
 BinomialDistribution (IntType t, double p=0.5)
 Constructs a binomial distribution with specified parameters.
 
 BinomialDistribution (const param_type &param)
 Constructs from a parameter set.
 
void reset ()
 Resets the distribution state.
 
IntType t () const
 Gets the number of trials.
 
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 maximum value that can be generated (t).
 
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 BinomialDistribution &lhs, const BinomialDistribution &rhs)
 
bool operator!= (const BinomialDistribution &lhs, const BinomialDistribution &rhs)
 

Detailed Description

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

Platform-independent binomial distribution.

Produces integer counts of successes in t independent Bernoulli trials, each with success probability p. For large t*p and t*(1-p), uses normal approximation for efficiency; otherwise uses direct simulation.

The binomial distribution models the number of successes in a fixed number of independent yes/no trials, such as number of heads in coin flips, defective items in a batch, or hits in a series of attempts. It's one of the most fundamental discrete distributions in probability theory.

Template Parameters
IntTypeInteger type for the result

Example usage:

PRNG rng(12345);
// Number of heads in 10 fair coin flips
int heads = coins(rng); // Returns 0-10
// Quality control: defects in batch of 100 (2% defect rate)
BinomialDistribution<int> defects(100, 0.02);
int defective_count = defects(rng);
// Success rate in RPG combat system
BinomialDistribution<int> hits(5, 0.7); // 5 attacks, 70% hit chance each
int successful_hits = hits(rng);
// Procedural generation: number of resources in region
BinomialDistribution<int> resources(20, 0.3);
int resource_count = resources(rng);
Platform-independent binomial distribution.
Definition PRNGDistribution.hpp:2651
Pseudo-random number generator for procedural generation.
Definition PRNG.hpp:48
Note
Returns values in [0, t].
Mean = t*p, Variance = t*p*(1-p).
Uses normal approximation when t*p >= 5 and t*(1-p) >= 5 for efficiency.
For p very close to 0 or 1, consider using Poisson approximation separately.

Member Typedef Documentation

◆ result_type

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

The type of values produced by the distribution.

Constructor & Destructor Documentation

◆ BinomialDistribution() [1/3]

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

Default constructor. Creates single trial distribution (t=1, p=0.5).

◆ BinomialDistribution() [2/3]

template<typename IntType = int>
Infinity::Engine::BinomialDistribution< IntType >::BinomialDistribution ( IntType  t,
double  p = 0.5 
)
inlineexplicit

Constructs a binomial distribution with specified parameters.

Parameters
tNumber of trials
pSuccess probability per trial

◆ BinomialDistribution() [3/3]

template<typename IntType = int>
Infinity::Engine::BinomialDistribution< IntType >::BinomialDistribution ( 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::BinomialDistribution< IntType >::max ( ) const
inline

Gets the maximum value that can be generated (t).

◆ min()

template<typename IntType = int>
result_type Infinity::Engine::BinomialDistribution< 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::BinomialDistribution< 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 successes in [0, t]

◆ operator()() [2/2]

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

Generates the next random value using provided parameters.

Uses two methods depending on parameters:

  1. Normal approximation (when t*p >= 5 and t*(1-p) >= 5): Uses continuity correction and samples from Normal(t*p, sqrt(t*p*(1-p))), then rounds to nearest integer and clamps to valid range.
  2. Direct simulation (small t or extreme p): Performs t independent Bernoulli trials and counts successes. Slower but exact for small numbers of trials.
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 successes in [0, param.t()]

◆ p()

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

Gets the success probability.

◆ param() [1/2]

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

Gets the current parameter set.

◆ param() [2/2]

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

Sets new parameters for the distribution.

Recreates the normal distribution for approximation if needed.

Parameters
paramThe new distribution parameters

◆ reset()

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

Resets the distribution state.

Resets the underlying normal distribution if it's being used for approximation.

◆ t()

template<typename IntType = int>
IntType Infinity::Engine::BinomialDistribution< IntType >::t ( ) const
inline

Gets the number of trials.

Friends And Related Symbol Documentation

◆ operator!=

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

◆ operator==

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