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

Platform-independent uniform integer 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

 UniformIntDistribution ()
 Default constructor. Creates a uniform distribution over [0, IntType::max()].
 
 UniformIntDistribution (IntType a, IntType b=std::numeric_limits< IntType >::max())
 Constructs a uniform distribution with specified bounds.
 
 UniformIntDistribution (const param_type &param)
 Constructs from a parameter set.
 
void reset ()
 Resets the distribution state.
 
IntType a () const
 Gets the lower bound of the distribution range.
 
IntType b () const
 Gets the upper bound of the distribution range.
 
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 (inclusive).
 
result_type max () const
 Gets the maximum value that can be generated (inclusive).
 
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 UniformIntDistribution &lhs, const UniformIntDistribution &rhs)
 
bool operator!= (const UniformIntDistribution &lhs, const UniformIntDistribution &rhs)
 

Detailed Description

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

Platform-independent uniform integer distribution.

Produces integer values uniformly distributed over a specified range [a, b] (inclusive). Uses Lemire's algorithm for fast, unbiased bounded random integers, which is both faster and more portable than the rejection sampling used by some standard library implementations.

This implementation guarantees identical results across all platforms when given the same PRNG seed, making it ideal for procedural generation that must be reproducible across different systems.

Template Parameters
IntTypeInteger type (signed or unsigned)

Example usage:

PRNG rng(12345);
int roll = dice(rng); // Random int from 1 to 6 inclusive
// Generate grid coordinates
UniformIntDistribution<int> coord_dist(0, 99);
int x = coord_dist(rng);
int y = coord_dist(rng);
// Works with unsigned types too
uint32_t id = id_gen(rng);
Pseudo-random number generator for procedural generation.
Definition PRNG.hpp:48
Platform-independent uniform integer distribution.
Definition PRNGDistribution.hpp:280
Note
Both bounds are inclusive; values are in [a, b], not [a, b).
Lemire's algorithm ensures perfect uniformity even when the range doesn't divide evenly into the generator's period.

Member Typedef Documentation

◆ result_type

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

The type of values produced by the distribution.

Constructor & Destructor Documentation

◆ UniformIntDistribution() [1/3]

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

Default constructor. Creates a uniform distribution over [0, IntType::max()].

◆ UniformIntDistribution() [2/3]

template<typename IntType = int>
Infinity::Engine::UniformIntDistribution< IntType >::UniformIntDistribution ( IntType  a,
IntType  b = std::numeric_limits<IntType>::max() 
)
inlineexplicit

Constructs a uniform distribution with specified bounds.

Parameters
aLower bound (inclusive)
bUpper bound (inclusive)

◆ UniformIntDistribution() [3/3]

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

Constructs from a parameter set.

Parameters
paramThe distribution parameters

Member Function Documentation

◆ a()

template<typename IntType = int>
IntType Infinity::Engine::UniformIntDistribution< IntType >::a ( ) const
inline

Gets the lower bound of the distribution range.

◆ b()

template<typename IntType = int>
IntType Infinity::Engine::UniformIntDistribution< IntType >::b ( ) const
inline

Gets the upper bound of the distribution range.

◆ max()

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

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

◆ min()

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

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

◆ operator()() [1/2]

template<typename IntType = int>
template<typename Generator >
result_type Infinity::Engine::UniformIntDistribution< 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 value uniformly distributed in [a, b] (inclusive)

◆ operator()() [2/2]

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

Generates the next random value using provided parameters.

Uses Lemire's nearly divisionless algorithm for fast, unbiased random integers. This method is faster than rejection sampling and produces perfectly uniform results even when the range doesn't divide evenly into 2^32.

The algorithm works by:

  1. Fast path for power-of-2 ranges using simple bit masking
  2. General path using 64-bit multiplication and rejection sampling only when needed
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 value uniformly distributed in [param.a(), param.b()] (inclusive)

◆ param() [1/2]

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

Gets the current parameter set.

◆ param() [2/2]

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

Sets new parameters for the distribution.

◆ reset()

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

Resets the distribution state.

No-op for uniform distributions as they maintain no internal state between generations.

Friends And Related Symbol Documentation

◆ operator!=

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

◆ operator==

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