Zero  0.1.0
Classes | Public Types | Public Member Functions | Private Attributes | Friends | List of all members
zero::uniform_int_distribution::biased_uniform_int_distribution< int_type > Class Template Reference

Distributes random numbers from a PRNG uniformly (but biased) distributed in a range. More...

#include <uniform_int_distribution.hpp>

Classes

struct  param_type
 Configuration of a random distribution facility. More...
 

Public Types

using result_type = int_type
 The output data type of this distribution facility. More...
 

Public Member Functions

 biased_uniform_int_distribution ()
 Constructs an unspecified biased uniform random integer distribution facility. More...
 
 biased_uniform_int_distribution (int_type lowerLimit, int_type upperLimit=std::numeric_limits< int_type >::max())
 Constructs a biased uniform random integer distribution facility with given lower and upper limits. More...
 
 biased_uniform_int_distribution (const param_type &parameters)
 Constructs a biased uniform random integer distribution facility based on a given configuration. More...
 
void reset () const noexcept
 Resets the internal state of the random distribution facility. More...
 
result_type a () const noexcept
 Returns the lower limit of the output range. More...
 
result_type b () const noexcept
 Returns the upper limit of the output range. More...
 
param_type param () const noexcept
 Returns the configuration of this random distribution facility. More...
 
void param (const param_type &parameters) noexcept
 Changes this biased uniform random integer distribution facility according the given configuration. More...
 
result_type min () const noexcept
 Returns the minimum integer value returned by this random distribution facility. More...
 
result_type max () const noexcept
 Returns the maximum integer value returned by this random distribution facility. More...
 
template<typename uniform_random_number_generator >
result_type operator() (uniform_random_number_generator &uniformRandomNumberGenerator) const
 Generates the next biased uniformly distributed random integer in the given range from the given PRNG. More...
 

Private Attributes

param_type _parameters
 The configuration of this random distribution facility. More...
 
int_type _offset
 The lower limit of output range of this random distribution facility. More...
 
int_type _range
 The length of the output range of this random distribution facility. More...
 
uint16_t _rangeBits
 The number of bits required to differentiate all the values in the output range of this random distribution facility. More...
 
boost::random::uniform_int_distribution< int_type > _fallbackDistribution
 The fallback random distribution facility. More...
 

Friends

bool operator== (const biased_uniform_int_distribution< result_type > &distribution0, const biased_uniform_int_distribution< result_type > &distribution1) noexcept
 Compares two biased uniform random integer distribution facilities for equality. More...
 
bool operator!= (const biased_uniform_int_distribution< result_type > &distribution0, const biased_uniform_int_distribution< result_type > &distribution1) noexcept
 Compares two biased uniform random integer distribution facilities for inequality. More...
 
template<class CharT , class Traits , class result_type >
std::basic_ostream< CharT, Traits > & operator<< (std::basic_ostream< CharT, Traits > &outputStream, const biased_uniform_int_distribution< result_type > &distribution)
 Serializes the given random distribution facility to the given stream. More...
 
template<class CharT , class Traits , class result_type >
std::basic_istream< CharT, Traits > & operator>> (std::basic_istream< CharT, Traits > &inputStream, const biased_uniform_int_distribution< result_type > &distribution)
 Deserializes a random distribution facility from the given stream and apply its parameters to the given random distribution facility. More...
 

Detailed Description

template<class int_type = int>
class zero::uniform_int_distribution::biased_uniform_int_distribution< int_type >

Distributes random numbers from a PRNG uniformly (but biased) distributed in a range.

This is a drop-in replacement for the std::uniform_int_distribution and the boost::random::uniform_int_distribution with the following differences:

Author
Max Gilbert
Note
This implementation using metaprogramming is a generalization of the algorithm "Integer Multiplication (Biased)" as presented by Melissa E. O’Neill in her shootout Efficiently Generating a Number in a Range
Template Parameters
int_typeThe integer data type of the output range.

Member Typedef Documentation

§ result_type

The output data type of this distribution facility.

Constructor & Destructor Documentation

§ biased_uniform_int_distribution() [1/3]

Constructs an unspecified biased uniform random integer distribution facility.

§ biased_uniform_int_distribution() [2/3]

template<class int_type = int>
zero::uniform_int_distribution::biased_uniform_int_distribution< int_type >::biased_uniform_int_distribution ( int_type  lowerLimit,
int_type  upperLimit = std::numeric_limits<int_type>::max() 
)
inlineexplicit

Constructs a biased uniform random integer distribution facility with given lower and upper limits.

Parameters
lowerLimitLower limit of the random distribution's output range
upperLimitUpper limit of the random distribution's output range

§ biased_uniform_int_distribution() [3/3]

template<class int_type = int>
zero::uniform_int_distribution::biased_uniform_int_distribution< int_type >::biased_uniform_int_distribution ( const param_type parameters)
inlineexplicit

Constructs a biased uniform random integer distribution facility based on a given configuration.

Parameters
parametersThe configuration providing the required parameters

Member Function Documentation

§ a()

template<class int_type = int>
zero::uniform_int_distribution::biased_uniform_int_distribution< int_type >::a ( ) const
inlinenoexcept

Returns the lower limit of the output range.

Returns
Lower limit of the output range

§ b()

template<class int_type = int>
zero::uniform_int_distribution::biased_uniform_int_distribution< int_type >::b ( ) const
inlinenoexcept

Returns the upper limit of the output range.

Returns
Upper limit of the output range

§ max()

template<class int_type = int>
zero::uniform_int_distribution::biased_uniform_int_distribution< int_type >::max ( ) const
inlinenoexcept

Returns the maximum integer value returned by this random distribution facility.

Returns
Upper limit of the output range

§ min()

template<class int_type = int>
zero::uniform_int_distribution::biased_uniform_int_distribution< int_type >::min ( ) const
inlinenoexcept

Returns the minimum integer value returned by this random distribution facility.

Returns
Lower limit of the output range

§ operator()()

template<class int_type = int>
template<typename uniform_random_number_generator >
zero::uniform_int_distribution::biased_uniform_int_distribution< int_type >::operator() ( uniform_random_number_generator &  uniformRandomNumberGenerator) const
inline

Generates the next biased uniformly distributed random integer in the given range from the given PRNG.

Template Parameters
uniform_random_number_generatorThe type of PRNG that should be used to obtain the used random number which is the entropy used for the generation of the biased uniformly distributed random integer.
Parameters
uniformRandomNumberGeneratorThe instance of uniform_random_number_generator that should be used to generate the next random integer with this distribution.
Returns
A biased uniformly distributed random integer generated with entropy i.e. a uniformly distributed random number generated using the given uniformRandomNumberGenerator .

§ param() [1/2]

template<class int_type = int>
zero::uniform_int_distribution::biased_uniform_int_distribution< int_type >::param ( ) const
inlinenoexcept

Returns the configuration of this random distribution facility.

Returns
The configuration of this random distribution facility

§ param() [2/2]

template<class int_type = int>
zero::uniform_int_distribution::biased_uniform_int_distribution< int_type >::param ( const param_type parameters)
inlinenoexcept

Changes this biased uniform random integer distribution facility according the given configuration.

Parameters
parametersThe configuration providing the required parameters

§ reset()

template<class int_type = int>
zero::uniform_int_distribution::biased_uniform_int_distribution< int_type >::reset ( ) const
inlinenoexcept

Resets the internal state of the random distribution facility.

This biased uniform random integer distribution facility does not have an internal state and therefore, this does nothing.

Friends And Related Function Documentation

§ operator!=

template<class int_type = int>
zero::uniform_int_distribution::biased_uniform_int_distribution< int_type >::operator!= ( const biased_uniform_int_distribution< result_type > &  distribution0,
const biased_uniform_int_distribution< result_type > &  distribution1 
)
friend

Compares two biased uniform random integer distribution facilities for inequality.

Parameters
distribution0A random distribution facility
distribution1Another random distribution facility
Returns
This is true iff the two random distribution facilities are not equal according to operator==() , otherwise this is false .

§ operator<<

template<class int_type = int>
template<class CharT , class Traits , class result_type >
zero::uniform_int_distribution::biased_uniform_int_distribution< int_type >::operator<< ( std::basic_ostream< CharT, Traits > &  outputStream,
const biased_uniform_int_distribution< result_type > &  distribution 
)
friend

Serializes the given random distribution facility to the given stream.

Serializes the given biased uniform random integer distribution facility to the given output stream by printing the required parameters (lower and upper limit of the output range) to the given stream.

Template Parameters
CharT
Traits
result_type
Parameters
outputStreamThe output stream to serialize the given random distribution facility to.
distributionThe random distribution facility to serialize to the given output stream.
Returns
The used output stream

§ operator==

template<class int_type = int>
zero::uniform_int_distribution::biased_uniform_int_distribution< int_type >::operator== ( const biased_uniform_int_distribution< result_type > &  distribution0,
const biased_uniform_int_distribution< result_type > &  distribution1 
)
friend

Compares two biased uniform random integer distribution facilities for equality.

Parameters
distribution0A random distribution facility
distribution1Another random distribution facility
Returns
This is true iff lower and upper limits of both random distribution facilities are equal, otherwise this is false .

§ operator>>

template<class int_type = int>
template<class CharT , class Traits , class result_type >
zero::uniform_int_distribution::biased_uniform_int_distribution< int_type >::operator>> ( std::basic_istream< CharT, Traits > &  inputStream,
const biased_uniform_int_distribution< result_type > &  distribution 
)
friend

Deserializes a random distribution facility from the given stream and apply its parameters to the given random distribution facility.

Deserializes a biased uniform random integer distribution facility from the given output stream by reading the required parameters (lower and upper limit of the output range) from the given stream and applying those to the given random distribution facility.

Template Parameters
CharT
Traits
result_type
Parameters
inputStreamThe input stream from which to deserialize the given random distribution facility.
distributionThe random distribution facility to deserialize the serialized random distribution facility into.
Returns
The used input stream

Member Data Documentation

§ _fallbackDistribution

template<class int_type = int>
zero::uniform_int_distribution::biased_uniform_int_distribution< int_type >::_fallbackDistribution
private

The fallback random distribution facility.

This is used when the entropy of one random number generated using the underlying PRNG is too low for the output range of this random distribution resulting in the need to collect entropy generating multiple random numbers using the underlying PRNG.

§ _offset

template<class int_type = int>
zero::uniform_int_distribution::biased_uniform_int_distribution< int_type >::_offset
private

The lower limit of output range of this random distribution facility.

In a first step, a random number in the range from 0 to _range is calculated and afterwards this is shifted by this offset.

§ _parameters

template<class int_type = int>
zero::uniform_int_distribution::biased_uniform_int_distribution< int_type >::_parameters
private

The configuration of this random distribution facility.

§ _range

template<class int_type = int>
zero::uniform_int_distribution::biased_uniform_int_distribution< int_type >::_range
private

The length of the output range of this random distribution facility.

In a first step, a random number in the range from 0 to this value is calculated and afterwards this is shifted by the _offset .

§ _rangeBits

template<class int_type = int>
zero::uniform_int_distribution::biased_uniform_int_distribution< int_type >::_rangeBits
private

The number of bits required to differentiate all the values in the output range of this random distribution facility.

This is equivalent to the bitwidth of the entropy required to generate one biased uniformly distributed random integer.


The documentation for this class was generated from the following file: