[random] Problems generating pseudorandom 64-bit ints across multiple computers
I am currently developing a distributed application where many clients need
to generate unique 64-bit random numbers which will be used as keys. I am
having problems getting boost to do this. For my first attempt, I tried
something like the following:
boost::mt19937 RandomNumberGenerator::s_algorithm;
boost::uniform_int<unsigned long long>
RandomNumberGenerator::s_range(
std::numeric_limits<unsigned long long>::min(),
std::numeric_limits<unsigned long long>::max());
boost::variate_generator
AMDG James Madison wrote:
I am currently developing a distributed application where many clients need to generate unique 64-bit random numbers which will be used as keys. I am having problems getting boost to do this. For my first attempt, I tried something like the following:
boost::mt19937 RandomNumberGenerator::s_algorithm;
boost::uniform_int<unsigned long long> RandomNumberGenerator::s_range( std::numeric_limits<unsigned long long>::min(), std::numeric_limits<unsigned long long>::max());
boost::variate_generator
RandomNumberGenerator::s_rng(RandomNumberGenerator::s_algorithm, RandomNumberGenerator::s_range);
I had two issues though:
1. Is this really giving me random 64 bit numbers, or is it just generating 32-bit numbers and then doing bit expansion?
mt19937 generates 32 bit integers. uniform_int combines multiple invocations of the underlying PRNG if necessary.
2. The values always started with the same number.
Trying to find a fix for #2, I though of using a hashed GUID as a seed. The problem is I noticed the seed value takes a 32 bit int, which essentially means a 1/2^32 chance of the first value colliding even if chosen completely randomly, which is unacceptable for my application.
There is also seed functions which takes a pair of iterators. In Christ, Steven Watanabe
Thanks for the reply, I tried implementing your suggestion of using a pair of iterators, but I think that I might be misinterpreting the source, and that it is still only using a 32-bit seed.
From looking at the source, the seed appears to be stored as the variable "i", which is of type int. The seed method using two iterators does a ton of mangling over the range, and assigned i to the result.
Am I misreading the source here, or no matter what I do, am I stuck with a
32 bit seed?
If so, is there a more suitable method to do what I am trying to
accomplish? I really need the seed to be (at least) 64 bits.
On Tue, Feb 24, 2009 at 9:28 PM, Steven Watanabe
AMDG
James Madison wrote:
I am currently developing a distributed application where many clients need to generate unique 64-bit random numbers which will be used as keys. I am having problems getting boost to do this. For my first attempt, I tried something like the following:
boost::mt19937 RandomNumberGenerator::s_algorithm;
boost::uniform_int<unsigned long long> RandomNumberGenerator::s_range( std::numeric_limits<unsigned long long>::min(), std::numeric_limits<unsigned long long>::max());
boost::variate_generator
RandomNumberGenerator::s_rng(RandomNumberGenerator::s_algorithm, RandomNumberGenerator::s_range);
I had two issues though:
1. Is this really giving me random 64 bit numbers, or is it just generating 32-bit numbers and then doing bit expansion?
mt19937 generates 32 bit integers. uniform_int combines multiple invocations of the underlying PRNG if necessary.
2. The values always started with the same number.
Trying to find a fix for #2, I though of using a hashed GUID as a seed. The problem is I noticed the seed value takes a 32 bit int, which essentially means a 1/2^32 chance of the first value colliding even if chosen completely randomly, which is unacceptable for my application.
There is also seed functions which takes a pair of iterators.
In Christ, Steven Watanabe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
AMDG James Madison wrote:
Thanks for the reply, I tried implementing your suggestion of using a pair of iterators, but I think that I might be misinterpreting the source, and that it is still only using a 32-bit seed.
From looking at the source, the seed appears to be stored as the variable "i", which is of type int. The seed method using two iterators does a ton of mangling over the range, and assigned i to the result.
I don't understand. I see this: template<class It> void seed(It& first, It last) { int j; for(j = 0; j < n && first != last; ++j, ++first) x[j] = *first; i = n; if(first == last && j < n) throw std::invalid_argument("mersenne_twister::seed"); } In Christ, Steven Watanabe
participants (2)
-
James Madison
-
Steven Watanabe