小编use*_*182的帖子

在C++中使用<random>

提前感谢您的阅读.我是C++的新手(虽然不是一般的编程)并且不太了解.我正在处理的问题需要大量高质量的随机数,默认的rand()函数是不够的.我尝试使用"随机"库但无法使其工作.以下简单代码:

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <random>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    mt19937 gen(random_device());
    normal_distribution<double> randn(0.0, 1.0);
    double t;
    for (int i = 0; i < 50; i++){
        t = randn(gen); //program compiles correctly if call to randn is omitted
        cout << t << "\n";
    }
    int a;
    cin >> a;

}
Run Code Online (Sandbox Code Playgroud)

无法编译,给我10个错误,大多数都是这样的:

error C2780: '_Rty std::_Nrand(_Engine &,float,_Rty)' : expects 3 arguments - 2 provided
Run Code Online (Sandbox Code Playgroud)

我尝试了其他发电机和概率分布 - 同样的问题.有任何想法吗?

c++ random

2
推荐指数
1
解决办法
339
查看次数

标签 统计

c++ ×1

random ×1