How to generate random 64 bit ints with boost random
Asked Answered
D

2

5

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.

Dicho answered 16/8, 2010 at 3:47 Comment(0)
S
7

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> > ...
Sholes answered 16/8, 2010 at 4:1 Comment(4)
Right, the clue is in the error message: [with IntType = int].Canny
And uint64_t can be found in <boost/cstdint.hpp>.Misapply
Sounded promising, but it now doesn't compile. See my updated question.Dicho
@Matt: Looks like you forgot to change the second instance of boost::uniform_int, like the answers reminds. :) (Or maybe I'm wrong.) A typedef would be helpful here, so you only needed to change it in one spot.Misapply
S
2

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.

Sip answered 16/8, 2010 at 4:2 Comment(1)
That sounded promising, but it now doesn't compile. See updated question.Dicho

© 2022 - 2024 — McMap. All rights reserved.