C++ 在全双量程std::uniform\u real\u分布上,始终返回inf
考虑以下最小示例:C++ 在全双量程std::uniform\u real\u分布上,始终返回inf,c++,c++11,random,C++,C++11,Random,考虑以下最小示例: #include <random> #include <iostream> int main (const int argC, char* argV[] ) { std::uniform_real_distribution<double> dist(std::numeric_limits<double>::lowest(), std::numeric_limits<double>::max());
#include <random>
#include <iostream>
int main (const int argC, char* argV[] ) {
std::uniform_real_distribution<double> dist(std::numeric_limits<double>::lowest(), std::numeric_limits<double>::max());
std::random_device gen;
std::cout << dist(gen) << std::endl;
return 0;
}
#包括
#包括
int main(常量int argC,char*argV[]){
标准::统一实分布区(标准::数值极限::最低(),标准::数值极限::最大();
std::随机_设备发电机;
std::couta的一个要求是两个边界之间的差值小于std::numeric_limits::max()
。因此,您的尝试是错误的
作为一种解决方法,您可以将其分为两个生成器,一个用于负数,另一个用于非负数。从两个生成器中随机选择一个
请记住,范围的上限不能作为生成的数字返回。@NateEldredge在N4849(C++20草稿)中,构造函数的描述(第26.6.8.2.2节第1段)将其列为一项要求。哦,它也在C++17、26.5.8.2.2(2)中。