【发布时间】:2015-07-06 15:58:02
【问题描述】:
我正在使用以下代码来生成 [0,1] 之间的指数分布和 [0,1] 之间的正态分布:
#include <iostream>
#include <algorithm>
#include "boost/random.hpp"
#include "boost/generator_iterator.hpp"
using namespace std;
int main()
{
typedef boost::mt19937 RNGType;
RNGType rng;
//for generating exponential distribution
boost::exponential_distribution<0,1> one_to_six;
boost::variate_generator< RNGType, boost::exponential_distribution<> >
dice(rng, one_to_six);
double number = dice();
cout<<"random number according to exponential distribution="<<number<<"\n";
//for generating normal distribution
boost::normal_distribution<0,1> one_to_six1;
boost::variate_generator< RNGType, boost::normal_distribution<> >
dice1(rng, one_to_six1);
double number1 = dice1();
cout<<"random number according to normal distribution="<<number<<"\n";
}
但我不知道为什么我的代码出错了。有人可以帮我弄清楚我哪里出错了。我正在使用 c++11。
我得到的错误是:
no known conversion for argument 2 from ‘int’ to ‘boost::normal_distribution<>’
按照 Barry 的建议,我尝试将代码更改为:
int main()
{
typedef boost::mt19937 RNGType;
RNGType rng;
//for generating exponential distribution
boost::exponential_distribution<double> one_to_six;;
boost::variate_generator< RNGType, boost::exponential_distribution<> >
dice(rng, one_to_six);
double number = dice();
cout<<"random number according to exponential distribution="<<number<<"\n";
//for generating normal distribution
boost::normal_distribution<double> one_to_six1;
boost::variate_generator< RNGType, boost::normal_distribution<> >
dice1(rng, one_to_six1);
double number1 = dice1();
cout<<"random number according to normal distribution="<<number<<"\n";
}
但我仍然收到错误:注意:boost::variate_generator::variate_generator(Engine, Distribution)
【问题讨论】:
-
你遇到了什么错误?
-
@JimLewis 我已经发布了错误。
-
您将
one_to_six定义为两次不同的类型。这是非法的。请发布实际代码? -
@nneonneo 感谢您指出...这是一个错字...抱歉