使用模板增强随机性
所以我尝试将 Boost.Random mt19937 生成器与模板一起使用。我的 c++ 有点生疏,但据我所知(并且文档,与 Boost 一样,不亚于模糊),它应该采用一个模板参数来指定它的返回类型(float / double)。
我现在不知道问题出在哪里......它全部适用于
或
并停止使用模板。
代码如下:
template <class T>
class SpikingMatrixHelper {
public:
SpikingMatrixHelper(const int seed);
T generateNumber(const T, const T) const;
private:
boost::mt19937 gen;
};
template <class T>
SpikingMatrixHelper<T>::SpikingMatrixHelper(const int seed) : gen(seed) {}
template <class T>
T SpikingMatrixHelper<T>::generateNumber(const T min, const T max) const {
boost::uniform_real<T> dist(min, max);
boost::variate_generator<boost::mt19937&, boost::uniform_real<T> > g(gen, dist);
return g();
}
这会在 variate_generator
构造中抛出
/path/ [line] error: no matching function for call to ‘boost::variate_generator<boost::random::mersenne_twister<unsigned int, 32, 624, 397, 31, 2567483615u, 11, 7, 2636928640u, 15, 4022730752u, 18, 3346425566u>&, boost::uniform_real<double> >::variate_generator(const mt19937&, boost::uniform_real<double>&)’
/path/ [line] note: candidates are:
/usr/include/boost/random/variate_generator.hpp:133:3: note: boost::variate_generator<Engine, Distribution>::variate_generator(Engine, Distribution) [with Engine = boost::random::mersenne_twister<unsigned int, 32, 624, 397, 31, 2567483615u, 11, 7, 2636928640u, 15, 4022730752u, 18, 3346425566u>&, Distribution = boost::uniform_real<double>]
/usr/include/boost/random/variate_generator.hpp:133:3: note: no known conversion for argument 1 from ‘const mt19937 {aka const boost::random::mersenne_twister<unsigned int, 32, 624, 397, 31, 2567483615u, 11, 7, 2636928640u, 15, 4022730752u, 18, 3346425566u>}’ to ‘boost::random::mersenne_twister<unsigned int, 32, 624, 397, 31, 2567483615u, 11, 7, 2636928640u, 15, 4022730752u, 18, 3346425566u>&’
/usr/include/boost/random/variate_generator.hpp:114:7: note: boost::variate_generator<boost::random::mersenne_twister<unsigned int, 32, 624, 397, 31, 2567483615u, 11, 7, 2636928640u, 15, 4022730752u, 18, 3346425566u>&, boost::uniform_real<double> >::variate_generator(const boost::variate_generator<boost::random::mersenne_twister<unsigned int, 32, 624, 397, 31, 2567483615u, 11, 7, 2636928640u, 15, 4022730752u, 18, 3346425566u>&, boost::uniform_real<double> >&)
/usr/include/boost/random/variate_generator.hpp:114:7: note: candidate expects 1 argument, 2 provided
,正如我所说,自从我完成 C++ 以来已经有一段时间了,Boost 文档还有很多需要改进的地方,所以任何提示都值得赞赏。 。
So I'm trying to use the Boost.Random mt19937 generator with templates. My c++ is a bit rusty, but from all I understand (and the doc, as always for Boost, is no less than vague) it should take a template argument that specifies it's return type (float / double).
I have no idea right now as to where the problem lies... It all worked with <double>
or <float>
and stopped working with the template.
Here's the code:
template <class T>
class SpikingMatrixHelper {
public:
SpikingMatrixHelper(const int seed);
T generateNumber(const T, const T) const;
private:
boost::mt19937 gen;
};
template <class T>
SpikingMatrixHelper<T>::SpikingMatrixHelper(const int seed) : gen(seed) {}
template <class T>
T SpikingMatrixHelper<T>::generateNumber(const T min, const T max) const {
boost::uniform_real<T> dist(min, max);
boost::variate_generator<boost::mt19937&, boost::uniform_real<T> > g(gen, dist);
return g();
}
This throws up at the variate_generator
construction with
/path/ [line] error: no matching function for call to ‘boost::variate_generator<boost::random::mersenne_twister<unsigned int, 32, 624, 397, 31, 2567483615u, 11, 7, 2636928640u, 15, 4022730752u, 18, 3346425566u>&, boost::uniform_real<double> >::variate_generator(const mt19937&, boost::uniform_real<double>&)’
/path/ [line] note: candidates are:
/usr/include/boost/random/variate_generator.hpp:133:3: note: boost::variate_generator<Engine, Distribution>::variate_generator(Engine, Distribution) [with Engine = boost::random::mersenne_twister<unsigned int, 32, 624, 397, 31, 2567483615u, 11, 7, 2636928640u, 15, 4022730752u, 18, 3346425566u>&, Distribution = boost::uniform_real<double>]
/usr/include/boost/random/variate_generator.hpp:133:3: note: no known conversion for argument 1 from ‘const mt19937 {aka const boost::random::mersenne_twister<unsigned int, 32, 624, 397, 31, 2567483615u, 11, 7, 2636928640u, 15, 4022730752u, 18, 3346425566u>}’ to ‘boost::random::mersenne_twister<unsigned int, 32, 624, 397, 31, 2567483615u, 11, 7, 2636928640u, 15, 4022730752u, 18, 3346425566u>&’
/usr/include/boost/random/variate_generator.hpp:114:7: note: boost::variate_generator<boost::random::mersenne_twister<unsigned int, 32, 624, 397, 31, 2567483615u, 11, 7, 2636928640u, 15, 4022730752u, 18, 3346425566u>&, boost::uniform_real<double> >::variate_generator(const boost::variate_generator<boost::random::mersenne_twister<unsigned int, 32, 624, 397, 31, 2567483615u, 11, 7, 2636928640u, 15, 4022730752u, 18, 3346425566u>&, boost::uniform_real<double> >&)
/usr/include/boost/random/variate_generator.hpp:114:7: note: candidate expects 1 argument, 2 provided
As I said, it's been some time since I've done c++, and the Boost doc leaves much to ask for, so any hints appreciated...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
generateNumber
不能是const
- 它会排列 Mersenne Twister。使该可变
,或使该函数成为非const
。generateNumber
cannot beconst
- it permutes the Mersenne Twister. Make thatmutable
, or make the function non-const
.