When I run this code:
double getRandomDouble() {
static std::mt19937 entropy_ = std::mt19937();
std::uniform_real_distribution<double> distribution;
distribution.param(typename decltype(distribution)::param_type(std::numeric_limits<double>::lowest(),
std::numeric_limits<double>::max()));
return distribution(entropy_);
}
It always returns infinity (at least in GCC8.1 & clang 11.0.1. In MSVC 14.16.27023 it asserts)
Here is a working demonstration in GodBolt
I would expect this function to return any random double value, what is happening here?
The choice of parameters violates the precondition of std::uniform_real_distribution
(c.f. §26.6.9.2.2.2).
The preconditions being a ≤ b
and b - a ≤ numeric_limits<T>::max()
, where a
and b
are the min and max of the distribution. Using numeric_limits<double>::lowest()
and numeric_limits<double>::max()
would go against this.
As suggested by Ted, using a = -max()/2.0
, b = max()/2.0
would be a better choice.