The C++11 way of generating random numbers is:
- Instantiate a random number engine
- Instantiate a random distribution
- Push the random numbers from the engine through the distribution
The problem is that both a random number engine and a random distribution are templated with respect to the type of arithmetic you are using.
How do these two types of arithmetic need to be related?
Can you use a 32 bit integer for the engine and a 64 bit integer for the distribution and opposite? What are the dangers? What about floating point types?
I hypothesize a guideline that the number of possible numbers generated by an engine should be greater or equal than the number of distinct random numbers you are hoping to get. Unfortunately I was not able to test my hypothesis, since on my computer uint_fast32_t
and uint_fast64_t
are the same and therefore both suggested engines for each of the three C++11 generators produce the same results.
The documentation on C++11 distributions like std::uniform_real_distribution or std::uniform_int_distribution is incomplete in this regard:
This section is incomplete. Reason: requirements on Generator
But for exapmple the gcc 4.7
implementation of uniform_real_distribution
is:
template<typename _UniformRandomNumberGenerator>
result_type
operator()(_UniformRandomNumberGenerator& __urng,
const param_type& __p)
{
__detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
__aurng(__urng);
return (__aurng() * (__p.b() - __p.a())) + __p.a();
}
Where the Adaptor is:
An adaptor class for converting the output of any Generator into the input for a specific Distribution.
The "any" sounds reassuring, but is it standard? I am especially worried about hidden overflows that are hard to detect and which may compromise the correctness of the distribution.