【问题标题】:Generate distributions in c++11在 c++11 中生成分布
【发布时间】: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 感谢您指出...这是一个错字...抱歉

标签: c++ c++11 boost


【解决方案1】:

始终发布并阅读编译器错误。在这种情况下,很清楚:

main.cpp:13:36: error: template argument for template type parameter must be a type
   boost::exponential_distribution<0,1> one_to_six;
                                   ^
/usr/local/include/boost/random/exponential_distribution.hpp:37:16: note: template parameter is declared here
template<class RealType = double>
               ^
main.cpp:20:31: error: template argument for template type parameter must be a type
   boost::normal_distribution<0,1> one_to_six;
                              ^
/usr/local/include/boost/random/normal_distribution.hpp:256:16: note: template parameter is declared here
template<class RealType = double>
               ^
2 errors generated.

boost::exponential_distribution 定义为:

template <class RealType = double,
          class Policy   = policies::policy<> >
class exponential_distribution;

所以你可能需要:

boost::exponential_distribution<double> one_to_six;

normal_distribution 变量也是如此。此外,它应该采用不同的名称,因为目前您在同一范围内有两个名为 one_to_six 的变量。

【讨论】:

  • 我按照您的建议修改了代码,但我仍然收到问题中编辑中指定的错误
  • @JannatArora 为我编译。什么版本的提升?
  • 非常感谢...也为我编译,但我无法获得 [0,1] 之间的指数分布 ..它生成的数字是 1.45
  • @JannatArora 如果它为你编译,你为什么说你仍然得到一个编译错误?
  • 抱歉我修改了我的问题..我之前犯了一个愚蠢的错误..现在它对我来说编译得很好......谢谢你......但我无法在 [ 0,1] ..它生成的数字是1.45
猜你喜欢
  • 1970-01-01
  • 2010-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-24
  • 2011-01-20
相关资源
最近更新 更多