I’m trying to maintain separate random seeds for different clients (from server app).
random_r/srandom_r(linux api) can’t be used because the code must compile both on mac/linux.
It seems I could use boost::random instead of random_r/srandom_r.
I’ve tried defining my random function(which is class member function) and supply the function to random_shuffle’s 3rd parameter.
random_shuffle( RandomAccessIterator first, RandomAccessIterator last,
RandomNumberGenerator& rand );
ptrdiff_t MyClass::_MyRandom(ptrdiff_t i)
{
int result;
boost::uniform_int<> numberInterval( 1, 10000);
boost::variate_generator< RNGType, boost::uniform_int<> >
dice(mRng, numberInterval); // mRng is boost::mt19937 type instance variable
result = dice();
// random_r(mRandomData, &result);
result = result % i;
return result;
}
what’s the proper form of bind() here?
mRng.seed(mRandomSeed); //mRandomSeed will be different for different clients.
//I'm trying to random_shuffle a vector with the random seed.
random_shuffle(v.begin(), v.end(), boost::bind(&MyClass::_MyRandom, _1));
Being not so familiar with boost::random boost::bind, i’m not so sure if my approach is going to work.
Any comment on direction would be also appreciated.
There is some code that does what you want here.
This previous SO question has simpler code, but uses a function pointer instead of a function object, which will not be as efficient. Here is some (not tested) code to try as the last argument to
random_shuffle: