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

Platform-independent piecewise constant distribution. More...

#include <PRNGDistribution.hpp>

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

 PiecewiseConstantDistribution ()
 Default constructor. Creates uniform distribution over [0, 1).
 
template<typename InputIteratorB , typename InputIteratorW >
 PiecewiseConstantDistribution (InputIteratorB first_b, InputIteratorB last_b, InputIteratorW first_w)
 Constructs from interval boundaries and density weights.
 
template<typename UnaryOperation >
 PiecewiseConstantDistribution (std::initializer_list< RealType > bl, UnaryOperation fw)
 Constructs from interval boundaries and weight function.
 
template<typename UnaryOperation >
 PiecewiseConstantDistribution (size_t nw, RealType xmin, RealType xmax, UnaryOperation fw)
 Constructs with evenly-spaced intervals and weight function.
 
 PiecewiseConstantDistribution (const param_type &param)
 Constructs from a parameter set.
 
void reset ()
 Resets the distribution state.
 
std::vector< RealType > intervals () const
 Returns the interval boundaries.
 
std::vector< double > densities () const
 Returns the density values.
 
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.
 
result_type max () const
 Gets the maximum value that can be generated.
 
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 PiecewiseConstantDistribution &lhs, const PiecewiseConstantDistribution &rhs)
 
bool operator!= (const PiecewiseConstantDistribution &lhs, const PiecewiseConstantDistribution &rhs)
 

Detailed Description

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

Platform-independent piecewise constant distribution.

Produces real values from a distribution defined by a piecewise constant density function. The range is divided into intervals, each with a constant probability density, allowing you to approximate arbitrary probability distributions or create custom probability landscapes for procedural generation.

This distribution is extremely useful for creating non-uniform spatial distributions, modeling multi-modal data, approximating complex probability distributions, or implementing custom probability functions that can't be expressed with standard distributions.

Template Parameters
RealTypeFloating point type (float or double)

Example usage:

PRNG rng(12345);
// Three intervals with different densities
std::vector<float> intervals = {0.0f, 1.0f, 2.0f, 3.0f};
std::vector<double> densities = {0.5, 2.0, 0.5}; // Middle interval more likely
intervals.begin(), intervals.end(),
densities.begin()
);
float value = dist(rng);
// Biome distribution: different probabilities in different elevation ranges
{0.0f, 100.0f, 500.0f, 1000.0f}, // Interval boundaries
[](float x) { return x < 250.0f ? 1.0 : 0.3; } // Lower elevations more common
);
// Resource density map with hot spots
auto density_fn = [](double x) {
return (x > 0.3 && x < 0.7) ? 5.0 : 1.0; // High density in middle
};
PiecewiseConstantDistribution<double> resources(10, 0.0, 1.0, density_fn);
Pseudo-random number generator for procedural generation.
Definition PRNG.hpp:48
std::vector< double > densities() const
Returns the density values.
Definition PRNGDistribution.hpp:4124
std::vector< RealType > intervals() const
Returns the interval boundaries.
Definition PRNGDistribution.hpp:4121
template class INFINITY_API_PUBLIC PiecewiseConstantDistribution< float >
Definition PRNGDistribution.cpp:65
template class INFINITY_API_PUBLIC PiecewiseConstantDistribution< double >
Definition PRNGDistribution.cpp:66
Note
Densities are automatically normalized to integrate to 1.
Returns values within the specified interval range.
Each interval has constant density - use PiecewiseLinearDistribution for smooth transitions.
Useful for approximating empirical distributions or complex PDFs.

Member Typedef Documentation

◆ result_type

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

The type of values produced by the distribution.

Constructor & Destructor Documentation

◆ PiecewiseConstantDistribution() [1/5]

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

Default constructor. Creates uniform distribution over [0, 1).

◆ PiecewiseConstantDistribution() [2/5]

template<typename RealType = double>
template<typename InputIteratorB , typename InputIteratorW >
Infinity::Engine::PiecewiseConstantDistribution< RealType >::PiecewiseConstantDistribution ( InputIteratorB  first_b,
InputIteratorB  last_b,
InputIteratorW  first_w 
)
inline

Constructs from interval boundaries and density weights.

Template Parameters
InputIteratorBIterator type for boundaries
InputIteratorWIterator type for weights
Parameters
first_bIterator to first boundary
last_bIterator past last boundary
first_wIterator to first weight

◆ PiecewiseConstantDistribution() [3/5]

template<typename RealType = double>
template<typename UnaryOperation >
Infinity::Engine::PiecewiseConstantDistribution< RealType >::PiecewiseConstantDistribution ( std::initializer_list< RealType >  bl,
UnaryOperation  fw 
)
inline

Constructs from interval boundaries and weight function.

Template Parameters
UnaryOperationFunction type
Parameters
blInitializer list of interval boundaries
fwWeight function

◆ PiecewiseConstantDistribution() [4/5]

template<typename RealType = double>
template<typename UnaryOperation >
Infinity::Engine::PiecewiseConstantDistribution< RealType >::PiecewiseConstantDistribution ( size_t  nw,
RealType  xmin,
RealType  xmax,
UnaryOperation  fw 
)
inline

Constructs with evenly-spaced intervals and weight function.

Template Parameters
UnaryOperationFunction type
Parameters
nwNumber of intervals
xminMinimum boundary
xmaxMaximum boundary
fwWeight function

◆ PiecewiseConstantDistribution() [5/5]

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

Constructs from a parameter set.

Parameters
paramThe distribution parameters

Member Function Documentation

◆ densities()

template<typename RealType = double>
std::vector< double > Infinity::Engine::PiecewiseConstantDistribution< RealType >::densities ( ) const
inline

Returns the density values.

◆ intervals()

template<typename RealType = double>
std::vector< RealType > Infinity::Engine::PiecewiseConstantDistribution< RealType >::intervals ( ) const
inline

Returns the interval boundaries.

◆ max()

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

Gets the maximum value that can be generated.

◆ min()

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

Gets the minimum value that can be generated.

◆ operator()() [1/2]

template<typename RealType = double>
template<typename Generator >
result_type Infinity::Engine::PiecewiseConstantDistribution< 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 random value according to the piecewise constant distribution

◆ operator()() [2/2]

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

Generates the next random value using provided parameters.

Uses a two-step process:

  1. Select an interval based on cumulative probabilities (binary search)
  2. Generate uniform value within the selected interval

This ensures the density within each interval is constant while different intervals can have different probabilities of selection.

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 from the piecewise constant distribution

◆ param() [1/2]

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

Gets the current parameter set.

◆ param() [2/2]

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

Sets new parameters for the distribution.

◆ reset()

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

Resets the distribution state.

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

Friends And Related Symbol Documentation

◆ operator!=

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

◆ operator==

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