我有一条线:
std::uniform_real_distribution<T> distribution(std::numeric_limits<T>::lowest(),
std::numeric_limits<T>::max());
Run Code Online (Sandbox Code Playgroud)
它编译但在Debug(VS 2017CE)上崩溃.我的猜测是,根据以下文件std::uniform_real_distribution:
需要
a ? b和b-a ? std::numeric_limits<RealType>::max()
当我的b是::max(),a是::lowest(),条件:
b-a ? std::numeric_limits<RealType>::max()
没有履行,因为b-a基本上是两倍的价值max.有没有解决这个问题,以便保持如此广泛的数字范围?::min()工作完美,但省略负值.仅浮动数字会出现问题.
如果我以下列方式生成浮点值:
template <typename T>
T RandomFromRange(T low, T high){
std::random_device random_device;
std::mt19937 engine{random_device()};
std::uniform_real_distribution<T> dist(low, high);
return dist(engine);
}
template <typename T>
T GetRandom(){
return RandomFromRange
(std::numeric_limits<T>::min(),std::numeric_limits<T>::max());
}
//produce floating point values:
auto num1 = GetRandom<float>();
auto num2 = GetRandom<float>();
auto num3 = GetRandom<float>();
//...
Run Code Online (Sandbox Code Playgroud)
是否有可能,我将永远得到一个NaN,Inf或-Inf?