Distributes random numbers from a PRNG uniformly (but biased) distributed in a range.
More...
|
| | 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 ¶meters) |
| | 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 ¶meters) 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...
|
| |
|
| 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...
|
| |
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:
- It offers a significantly higher performance for fast PRNGs.
The output of this distribution facility is not perfectly uniform—instead it is biased based on the given range and PRNG. If this would be used with a given integer range \([0,5]\) and with a PRNG returning uniformly distributed 3-bit integers, the following distribution would be achieved:
- \(0 \longrightarrow \frac{2}{8}\) from the uniformly distributed random integers 0 and 6
- \(1 \longrightarrow \frac{2}{8}\) from the uniformly distributed random integers 1 and 7
- \(2 \longrightarrow \frac{1}{8}\) from the uniformly distributed random integer 2
- \(3 \longrightarrow \frac{1}{8}\) from the uniformly distributed random integer 3
- \(4 \longrightarrow \frac{1}{8}\) from the uniformly distributed random integer 4
- \(5 \longrightarrow \frac{1}{8}\) from the uniformly distributed random integer 5
The output distribution is unbiased when the output range is a divisor of the PRNG's range and it is almost unbiased when the output range is much smaller than the PRNG's range.
- In contrast to
std::uniform_int_distribution , it supports PRNGs generating random floating point numbers.
- The bitwidth and range (only for PRNGs generating random integers) of the given PRNG's random numbers need to be higher than the one of the given output range biased_uniform_int_distribution::_range. Therefore, this distribution facility does not need to support multiple calls to the given PRNG for the generation of one random number in the given range.
- It uses the
boost::random::detail::uniform_int_float to transform random floating point numbers from the respective PRNG to uniformly distributed random integers.
- It requires compiler support for 128-bit arithmetic when
int_type has 64-bit width.
- 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_type | The integer data type of the output range. |
template<class int_type = int>
template<class CharT , class Traits , class result_type >
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
-
- Parameters
-
| outputStream | The output stream to serialize the given random distribution facility to. |
| distribution | The random distribution facility to serialize to the given output stream. |
- Returns
- The used output stream
template<class int_type = int>
template<class CharT , class Traits , class result_type >
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
-
- Parameters
-
| inputStream | The input stream from which to deserialize the given random distribution facility. |
| distribution | The random distribution facility to deserialize the serialized random distribution facility into. |
- Returns
- The used input stream