我需要经常使用随机数生成器,而不是每次需要它时都编写它,我想创建一个类,以便我可以将它链接到其他程序。我目前的代码是这样的:
#include <boost/random.hpp>
#include <iostream>
int main() {
boost::mt19937 rng(0);
boost::normal_distribution<float> dist_normal(0.0f,1.0f);
boost::variate_generator<boost::mt19937, boost::normal_distribution<float> > rand_normal(rng, dist_normal);
for(int i = 0 ; i < 10; ++i)
std::cout << rand_normal() << std::endl;
return 0;
}
但现在我想像这样分离接口和实现:
#include <boost/random.hpp>
#include <iostream>
int main() {
//This goes in the header file as private members of class
boost::mt19937 rng;
boost::normal_distribution<float> dist_normal;
boost::variate_generator<boost::mt19937, boost::normal_distribution<float> > rand_normal;
//This is then in the constructor in the cpp file
rng = boost::mt19937(0);
dist_normal = boost::normal_distribution<float>(0.0f,1.0f);
rand_normal = boost::variate_generator<boost::mt19937, boost::normal_distribution<float> >(rng, dist_normal);
for(int i = 0 ; i < 10; ++i)
std::cout << rand_normal() << std::endl;
return 0;
}
但这不会编译,它给了我以下错误(哦,破译提升错误的乐趣):
test2.cpp: In function ‘int main()’:
test2.cpp:7:79: error: no matching function for call to ‘boost::random::variate_generator<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>, boost::random::normal_distribution<float> >::variate_generator()’
test2.cpp:7:79: note: candidates are:
/usr/include/boost/random/variate_generator.hpp:69:5: note: boost::random::variate_generator<Engine, Distribution>::variate_generator(Engine, Distribution) [with Engine = boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>, Distribution = boost::random::normal_distribution<float>]
/usr/include/boost/random/variate_generator.hpp:69:5: note: candidate expects 2 arguments, 0 provided
/usr/include/boost/random/variate_generator.hpp:51:7: note: boost::random::variate_generator<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>, boost::random::normal_distribution<float> >::variate_generator(const boost::random::variate_generator<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>, boost::random::normal_distribution<float> >&)
/usr/include/boost/random/variate_generator.hpp:51:7: note: candidate expects 1 argument, 0 provided
在这里我被卡住了,据我所知,第二个候选者是复制构造函数,但是我应该如何阅读第一个候选者以及我应该如何更改我的代码以使其工作?提前致谢!
编辑:
工作解决方案,感谢 muehlbau。
boost_rng.c:
#include "boost_rng.hpp"
BoostRNG::BoostRNG(uint _seed) : seed(_seed),
rng(boost::mt19937(_seed)),
dist_normal(boost::normal_distribution<float>(0.0f,1.0f)),
rand_normal(boost::variate_generator<boost::mt19937&, boost::normal_distribution<float> >(rng, dist_normal))
{}
float BoostRNG::normal() {
return rand_normal();
}
和 boost_rng.hpp:
#include <boost/random.hpp>
class BoostRNG {
private:
uint seed;
boost::mt19937 rng;
boost::normal_distribution<float> dist_normal;
boost::variate_generator<boost::mt19937&, boost::normal_distribution<float> > rand_normal;
public:
BoostRNG(uint seed);
float normal();
};
请注意,它也适用于引用引擎,让我对多个生成器使用相同的引擎;)