C++ 在C+中生成超出范围的随机数+;使用tr1

C++ 在C+中生成超出范围的随机数+;使用tr1,c++,random,c++11,tr1,uniform,C++,Random,C++11,Tr1,Uniform,我试图从[0,1]范围内的实数的均匀分布生成数字。但编译器正在生成超出[0,1)范围的数字 代码如下: int main(void) { // Solver solve; mt19937 mteng; mteng.seed(rdtsc()); uniform_real<double> uniRealD; double randomNum; for (int index = 0; index < 10; index++){

我试图从[0,1]范围内的实数的均匀分布生成数字。但编译器正在生成超出[0,1)范围的数字

代码如下:

int main(void) {
    // Solver solve;

    mt19937 mteng;
    mteng.seed(rdtsc());
    uniform_real<double> uniRealD;

    double randomNum;

    for (int index = 0; index < 10; index++){
        randomNum = uniRealD(mteng);

        if(randomNum<0.5)
            cout<<index<<" no. random number is: "<<randomNum<<endl;
        else
            cout<<"number generate is not in range"<<endl;
    }

    return 0;
}
int main(无效){
//求解器求解;
mt19937 mteng;
mteng.seed(rdtsc());
统一实统一体;
双随机数;
对于(int-index=0;index<10;index++){
randomNum=uniRealD(mteng);

if(randomNum
if(randomNum您的代码不应该这样做。可能是实现中的错误。什么编译器和库版本?请尝试从tr1转移到C++11。

我必须去掉您的种子函数,
rdtsc()
,制作一些include,使用命名空间std引入
,将0.5更改为1.0,并将
uniform\u real
更改为
uniform\u real\u distribution
,但之后,使用libc++,我得到:

#include <random>
#include <iostream>

using namespace std;

int main(void) {
    // Solver solve;

    mt19937 mteng;
    mteng.seed(0);
    uniform_real_distribution<double> uniRealD;

    double randomNum;

    for (int index = 0; index < 10; index++){
        randomNum = uniRealD(mteng);

        if(randomNum<1.0)
            cout<<index<<" no. random number is: "<<randomNum<<endl;
        else
            cout<<"number generate is not in range"<<endl;
    }

    return 0;
}

0 no. random number is: 0.592845
1 no. random number is: 0.844266
2 no. random number is: 0.857946
3 no. random number is: 0.847252
4 no. random number is: 0.623564
5 no. random number is: 0.384382
6 no. random number is: 0.297535
7 no. random number is: 0.056713
8 no. random number is: 0.272656
9 no. random number is: 0.477665
#包括
#包括
使用名称空间std;
内部主(空){
//求解器求解;
mt19937 mteng;
种子(0);
均匀实分布;
双随机数;
对于(int-index=0;index<10;index++){
randomNum=uniRealD(mteng);

如果(随机数你得到了什么数字?以下是数字:2.59366e+09,2.34439e+09,3.48634e+09,3.36609e+09,3.68728e+09,1.83477e+09,3.47004e+09,1.14211e+09,2.24305e+09,1.05343e+09,你使用的编译器/版本是什么?gcc 4.6.1库工作得很好,只是这一代不受欢迎。帮我一个忙,修改一下
uniform\real
均匀实分布
并再次测试。但这不会对生成的数字产生任何影响,对吗?这样做了,但仍然是一样的。我甚至尝试了均匀实分布的范围(0,1)但即使这样也没有任何效果。@cHaTrU确实解决了这个问题。在IDEOne的GCC4.5.1上,使用TR1会产生错误,但更改为普通的
std
会给出正确的结果:传递了该标志,并将库更改为随机,将分发类名更改为
code
uniform\u real\u distribution
code
,它成功了lib这种古怪行为的原因是什么?@cHaTrU这是一个bug。他们有自己的原因:vP@cHaTrU你可能应该投票并选择这个答案…霍华德没有提到TR1是问题所在。伙计,我会投票支持你的答案,因为我有权这么做!至于标记答案,我们所有人都同时调试了这个问题Howard刚刚发布了一份完整的工作副本。是的,seed还可以。我甚至建议你使用它。几乎每次都使用唯一的seed。至于错误,前面提到的库有问题。谢谢。@cHaTrU我喜欢获取seed的两种方法是(详细)
mt19937(std::chrono::high_resolution\u clock::now().time\u since\u epoch().count())
或(短)
mt19937((std::random\u device())())
为什么需要将
统一实分布更改为
统一实分布?
#include <random>
#include <iostream>

using namespace std;

int main(void) {
    // Solver solve;

    mt19937 mteng;
    mteng.seed(0);
    uniform_real_distribution<double> uniRealD;

    double randomNum;

    for (int index = 0; index < 10; index++){
        randomNum = uniRealD(mteng);

        if(randomNum<1.0)
            cout<<index<<" no. random number is: "<<randomNum<<endl;
        else
            cout<<"number generate is not in range"<<endl;
    }

    return 0;
}

0 no. random number is: 0.592845
1 no. random number is: 0.844266
2 no. random number is: 0.857946
3 no. random number is: 0.847252
4 no. random number is: 0.623564
5 no. random number is: 0.384382
6 no. random number is: 0.297535
7 no. random number is: 0.056713
8 no. random number is: 0.272656
9 no. random number is: 0.477665