We know that the random
module in python use MT19937 to generate a 32-bit random number, for example:
>>> import random
>>> random.seed(123)
>>> random.getrandbits(32)
224899942L
>>> random.getrandbits(32)
1149664691L
>>> random.getrandbits(32)
374463918L
>>> random.getrandbits(32)
3302642556L
The output is the same as the MT19937's output when i set the same seed.
However, if i want to generate a random number less than 32-bit, for example, if i want to use random.getrandbits(1) to generate a 1-bit random number, at first i think the result is just random.getrandbits(32)&1
, but when i put into practice, i found it's not so:
>>> import random
>>> random.seed(123)
>>> random.getrandbits(1)
0L
>>> random.getrandbits(1)
0L
>>> random.getrandbits(1)
0L
>>> random.getrandbits(1)
1L
Now i have no idea why the output become 0,0,0,1...
, Where do these numbers come from?
And as we all know, if we get 624 consecutive 32-bit random numbers generated by MT19937RNG, we can easily recovered the seed and calculated any random number, but if we can only get consecutive random numbers less than 32-bit like 1-bit, can we still break the MT19937RNG?
Aucun commentaire:
Enregistrer un commentaire