Is it safe to take only a few bits from a number obtained with a Mersenne Twister

197 views Asked by At

I have to work with some code produced by an employee that is now retired, and I have some few strange things concerning random numbers. At some points, he shifted the value returned by a PRNG 10 bits to the right and then use a mask on this value.

I have already seen on the internet that some PRNG have poor randomness properties with some bits in the number they generate (like the last one, simply alternating between 1 and 0), but I've searched if some litterture existed for such problems on the Mersenne Twister, but I haven't found any. Does anyone know something about this ?

2

There are 2 answers

0
galinette On BEST ANSWER

Normally, any bit should be random, this is a property of the Mersenne twister.

However (I do not know MT very deeply) you may have long-term dependence between some bits. It is recommended to use the library functions for setting the integer range, rather than arranging the bits yourself, or you never know the complex properties it may get.

If you use the c++11 standard library, just use std::mt19937 together with std::uniform_int_distribution

4
ANSI C Mastah On

I am not sure about the Mersenne Twister in particular, but what comes to mind is the typical advice one gets when trying to get a random integer in the range [0, n). If you have a PRNG returning integers with a larger range than n, you should never use modulo to reduce range like

x = rand() % n;

but ought to rescale the number

x = (int) floor(((double) rand()) / ((double) RAND_MAX)) * n);

instead. The reason is that the most significant bits of the pseudo-random number typically are more random than the lesser bits, so while the modulo operation keeps things nice and floating-point free, it also discards those precious significant bits.

While I do not know what the code you mentioned attempted to do, it might be that the shift-to-right plus masking could have been to reduce the range of the random numbers in a way that discards the least significant bits.