C++ std::mt19937和std::均匀实分布每次返回边界值

C++ std::mt19937和std::均匀实分布每次返回边界值,c++,c++11,random,C++,C++11,Random,好的,我有一些RNG代码(当一切都说了和做了)可以归结为: #include <limits> #include <random> #include <chrono> #include <iostream> double randomValue() { // Seed a Mersenne Twister (good RNG) with the current system time std::mt19937 generator(

好的,我有一些RNG代码(当一切都说了和做了)可以归结为:

#include <limits>
#include <random>
#include <chrono>
#include <iostream>

double randomValue() {
    // Seed a Mersenne Twister (good RNG) with the current system time
    std::mt19937 generator(std::chrono::system_clock::now().time_since_epoch().count());

    std::uniform_real_distribution<double> dist(
        std::numeric_limits<double>::lowest(),
        std::numeric_limits<double>::max()
    );

    // Problem lives here
    for (unsigned int i = 0; i < 30; i++)
        std::cout << dist(generator) << "\n";
}
#包括
#包括
#包括
#包括
双随机值(){
//在当前系统时间内播种Mersenne捻线机(良好RNG)
std::mt19937生成器(std::chrono::system_clock::now().time_since_epoch().count());
标准:均匀实分布区(
标准::数值限制::最低(),
标准::数值限制::最大值()
);
//问题就在这里
for(无符号整数i=0;i<30;i++)
标准::cout根据:

需要:a≤ b和b− A.≤ 数值限制::max()。

在您的情况下,b-a大于允许的范围。

您是否尝试过
srand(时间(空))
?我从未使用过boost的随机对象,但我被教导必须始终为rng设定种子。这就是rng的arg所做的-使用当前系统时间设定种子。也就是说,我将编辑问题以使其更清楚。在统一的真实分布中,可能有“b-a”变成“inf”的地方。请检查您的std工具初始化。您是否尝试过只使用
1.0
5.0
或其他东西,而不是使用
lower()
max()
Requires: a ≤ b and b − a ≤ numeric_limits<RealType>::max().