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

I need to work with some code created by an employee who is now retired, and I have some strange things about random numbers. In some cases, he shifted the value returned by PRNG 10 bits to the right, and then used the mask for that value.

I have already seen on the Internet that some PRNGs have bad randomness properties with some bits in the amount that they generate (for example, the last, just alternating between 1 and 0), but I was looking if there was some kind of litterture for such problems on Mersenne Twister, but I did not find them. Does anyone know something about this?

+5
source share
2 answers

Usually any bit should be random, this is a Mersenne twister property.

However (I do not know MT very deeply) you may have a long-term relationship between some bits. It is recommended that you use library functions to set the integer range, rather than to place the bits themselves, or you never know the complex properties that it can get.

If you are using the standard C ++ 11 library just use std :: mt19937 along with std :: uniform_int_distribution

+1
source

I'm not sure about Mersenne Twister in particular, but what comes to mind is the typical advice that comes up when trying to get a random integer in the range [0, n). If you have PRNGs that return integers with a larger range than n, you should never use modulo to reduce the range, for example

x = rand() % n; 

but you need to rescale the number

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

instead of this. The reason is that the most significant pseudo-random number bits are usually more random than the low-order bits, therefore, while the modulo operation retains good floating-point results, it also discards these precious significant bits.

As long as I don’t know what the code you mentioned is done, it could be that masking the shift from right to left could be to reduce the range of random numbers in such a way as to discard the least significant bits.

-1
source

Source: https://habr.com/ru/post/1207671/


All Articles