相关疑难解决方法(0)

Uniform_real不接受numeric_limits :: lowest()

我有一条线:

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 ? bb-a ? std::numeric_limits<RealType>::max()

当我的b::max(),a::lowest(),条件:

b-a ? std::numeric_limits<RealType>::max()

没有履行,因为b-a基本上是两倍的价值max.有没有解决这个问题,以便保持如此广泛的数字范围?::min()工作完美,但省略负值.仅浮动数字会出现问题.

c++ random c++11

8
推荐指数
2
解决办法
228
查看次数

"最小到最大"均匀实际分布会产生Inf,-Inf还是NaN?

如果我以下列方式生成浮点值:

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

c++ random floating-point nan uniform-distribution

6
推荐指数
2
解决办法
503
查看次数

标签 统计

c++ ×2

random ×2

c++11 ×1

floating-point ×1

nan ×1

uniform-distribution ×1