C++ 在c+中生成分布+;11
我使用以下代码生成[0,1]之间的指数分布和[0,1]之间的正态分布:C++ 在c+中生成分布+;11,c++,c++11,boost,C++,C++11,Boost,我使用以下代码生成[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; //f
#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";
}
#包括
#包括
#包括“boost/random.hpp”
#包括“boost/generator_iterator.hpp”
使用名称空间std;
int main()
{
typedef boost::mt19937 RNGType;
rng型rng;
//生成指数分布的方法
boost::指数分布1到6;
boost::变量发生器
骰子(rng,一到六);
双倍数字=骰子();
cout始终发布并读取编译器错误。在这种情况下,很明显:
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<double> one_to_six;
boost::指数分布1到6;
你的变量也一样。此外,应该使用不同的名称,因为目前在同一范围内有两个名为1到6
的变量。你遇到了什么错误?@JimLewis我发布了错误。你将1到6
定义为两种不同的类型。这是非法的。请发布实际代码?@nneonneo感谢您指出…这是一个打字错误…很抱歉,我按照您的建议修改了代码,但我仍然收到了我的question@JannatArora为我编译。什么版本的boost?非常感谢…也为我编译,但我无法获得[0,1]之间的指数分布…它生成的数字是1。45@JannatArora如果它为您编译,为什么您说您仍然得到编译错误?抱歉,我修改了我的问题..我之前犯了一个愚蠢的错误..现在它为我编译得很好…感谢您…但我无法得到[0,1]之间的指数分布…它生成的数字是1.45
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.
template <class RealType = double,
class Policy = policies::policy<> >
class exponential_distribution;
boost::exponential_distribution<double> one_to_six;