Fixed-precision random numbers
Fixed-precision integers and floating point types are generated by concatenation and/or truncation of one or more samples from a uniform random number generator. If the requested bit width W is greater than the maximum intrinsic precision of the integer or of the floating point number to be generated, the number of significant bits requested from the random number generator is accordingly decreased.
The integer and floating point generation functions are, respectively:
template<typename UIntType, std::size_t W, typename RngType>
UIntType generate_random_integer(RngType& rng);
template<typename RealType, std::size_t W, typename RngType>
RealType generate_random_real(RngType& rng);
The returned numbers are within [0, 2ᵂ-1] for integers and within [0, 1) for floating point values.
These functions offer essentially the same functionality as the
independent_bits_engine class and generate_canonical() function of
the C++ standard library, with the following notable differences:
-
they are faster due to their use use of bit-level rather than arithmetic operations,
-
their implementation is hopefully more sound, meaning in particular that
generate_random_real()should never return 1 (see LWG issue 2524), -
they are less flexible: the RNG ideally needs to generate values between 0 and a power of 2 less 1, though a minimum value of 1 and/or a maximum value equal to a power of 2 less 2 are tolerated (the bit correlations introduced by the relaxed requirement is usually weak enough to be ignored),
-
floating point numbers generation is in theory less generic: the platform must define an unsigned type able to store either W bits or the number of significant bits of the floating point type, whichever is less.
Template parameters
| Parameter | Description |
UIntType | Unsigned integer type to be generated |
RealType | Floating point type to be generated |
W | Precision (in bits) of the generated number |
RngType | A type meeting the requirements of a C++11 uniform random number generator with the additional requirement that it should produce independent bits; this would in principle mean that its minimum value should be 0 and its maximum value a power of 2 less 1, but for the sake of practicality a minimum value of 1 and/or a maximum value equal to a power of 2 less 2 is tolerated (the bit correlations introduced by the relaxed requirement is usually weak enough to be ignored) |