如何使用 boost random 生成随机 64 位整数

发布于 2024-09-14 07:28:31 字数 1652 浏览 0 评论 0原文

我正在尝试使用 boost random 生成随机 64 位无符号整数, 但我遇到了uniform_int 的断言失败。

struct timeval tv;
boost::mt19937 randGen(tval.tv_usec);
boost::uniform_int<> uInt64Dist(0, std::numeric_limits<uint64_t>::max());
boost::variate_generator<boost::mt19937&, boost::uniform_int<> > getRand(randGen, uInt64Dist);
uint64_t clock_seq_= getRand();

这是第 3 行的输出。

main:/usr/include/boost/random/uniform_int.hpp:48: boost::uniform_int<IntType>::uniform_int(IntType, IntType) [with IntType = int]: Assertion `min_arg <= max_arg' failed.

编辑:根据您的答案,我尝试使用以下内容指定大小:

boost:uniform_int<uint64_t> ....

但出现以下编译错误:

spec.cpp: In member function ‘void Specifier::initialize()’:
spec.cpp:58: error: no matching function for call to ‘boost::variate_generator<boost::mt19937&, boost::uniform_int<int> >::variate_generator(boost::mt19937&, boost::uniform_int<long unsigned int>&)’
/usr/include/boost/random/variate_generator.hpp:97: note: candidates are: boost::variate_generator<Engine, Distribution>::variate_generator(Engine, Distribution) [with Engine = boost::mt19937&, Distribution = boost::uniform_int<int>]
/usr/include/boost/random/variate_generator.hpp:87: note:                 boost::variate_generator<boost::mt19937&, boost::uniform_int<int> >::variate_generator(const boost::variate_generator<boost::mt19937&, boost::uniform_int<int> >&)
make: *** [spec.o] Error 1

编辑:好的,错过了 boost::uniform_int 的第二个实例。一旦我得到了他们两个,一切就都过去了。

I'm trying to generate a random 64bit unsigned integer using boost random,
but I'm getting an assertion failure with uniform_int.

struct timeval tv;
boost::mt19937 randGen(tval.tv_usec);
boost::uniform_int<> uInt64Dist(0, std::numeric_limits<uint64_t>::max());
boost::variate_generator<boost::mt19937&, boost::uniform_int<> > getRand(randGen, uInt64Dist);
uint64_t clock_seq_= getRand();

Here is what gets output at line 3.

main:/usr/include/boost/random/uniform_int.hpp:48: boost::uniform_int<IntType>::uniform_int(IntType, IntType) [with IntType = int]: Assertion `min_arg <= max_arg' failed.

EDIT: Based on your answers, I tried to specify the size with below:

boost:uniform_int<uint64_t> ....

But I get the following compilation error:

spec.cpp: In member function ‘void Specifier::initialize()’:
spec.cpp:58: error: no matching function for call to ‘boost::variate_generator<boost::mt19937&, boost::uniform_int<int> >::variate_generator(boost::mt19937&, boost::uniform_int<long unsigned int>&)’
/usr/include/boost/random/variate_generator.hpp:97: note: candidates are: boost::variate_generator<Engine, Distribution>::variate_generator(Engine, Distribution) [with Engine = boost::mt19937&, Distribution = boost::uniform_int<int>]
/usr/include/boost/random/variate_generator.hpp:87: note:                 boost::variate_generator<boost::mt19937&, boost::uniform_int<int> >::variate_generator(const boost::variate_generator<boost::mt19937&, boost::uniform_int<int> >&)
make: *** [spec.o] Error 1

EDIT: ok, missed the second instance of boost::uniform_int. Once I got both of them, it's all go.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

多情癖 2024-09-21 07:28:31

uniform_int 默认值以 int 作为值类型。请改用以下内容:

boost::uniform_int<uint64_t> ...

以下行也是如此:

boost::variate_generator<boost::mt19937&, boost::uniform_int<uint64_t> > ...

uniform_int defaults to int as the value type. Use the following instead:

boost::uniform_int<uint64_t> ...

The same goes for the following line:

boost::variate_generator<boost::mt19937&, boost::uniform_int<uint64_t> > ...
初见 2024-09-21 07:28:31

您需要在 boost::uniform_int<> 声明中指定您使用的是 64 位整数类型。否则默认为32位类型。

you need to specify in your declaration of boost::uniform_int<> that you are using a 64 bit integer type. Otherwise it defaults to a 32 bit type.

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