使用模板增强随机性

发布于 2024-11-19 15:24:17 字数 2755 浏览 3 评论 0原文

所以我尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

全部不再 2024-11-26 15:24:18

generateNumber 不能是 const - 它会排列 Mersenne Twister。使该可变,或使该函数成为非const

generateNumber cannot be const - it permutes the Mersenne Twister. Make that mutable, or make the function non-const.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文