【发布时间】:2021-03-29 12:22:06
【问题描述】:
当我运行这段代码时:
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_);
}
它总是返回无穷大(至少在 GCC8.1 和 clang 11.0.1 中。在 MSVC 14.16.27023 中它断言)
Here is a working demonstration in GodBolt
我希望这个函数返回任何随机的双精度值,这里发生了什么?
【问题讨论】:
-
将
lowest()更改为min() -
@TedLyngmo 但它不会做我想做的事,因为它只会返回正值......
-
啊...好吧,
-max()/2.0, max()/2.0可能是一个不错的范围。 example -
@TedLyngmo 虽然这可能会“解决”问题,但它仍然不能解释发生了什么......
-
来自rand.dist.uni.real#1
p(x|a,b)=1/(b−a),但max()-lowest()使结果有问题:-/
标签: c++ random numeric-limits