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

标签 c++ boost boost-random

我正在尝试使用 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 的第二个实例。一旦我得到了它们,一切就都结束了。

最佳答案

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

boost::uniform_int<uint64_t> ...

下一行也是如此:

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

关于c++ - 如何使用 boost random 生成随机 64 位整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3490461/

相关文章:

c++ - 如何为 Eigen::EigenSolver 使用 Eigen::Map 对象?

c++ - DLL加载库-错误代码126

c++ - 使用 boost::program_options 和 push_back 阅读到 std::vector?

c++ - MSVC 和 boost::lambda::bind 错误:T0:不允许标准参数

c++ - 在 Linux Mint 上使用 CMake 找不到 boost

c++ - 启动 Mersenne twister PRNG

c++ - 使用模板 boost 随机性

c++ - 浮点值比较失败

c++ - 程序不识别智能指针

c++ - 对 long double 的(伪)随机数生成器使用 boost