I use the random.seed()
to set a seed for random
module, for example:
>>> import random
>>> random.seed(123456)
Then i use random.getstate()
to get the MT19937 array, it should be 624 32-bit integers:
>>> MT = random.getstate()[1][:-1]
>>> len(MT)
624
But when i check the value of these 624 numbers, i have no idea what is the relationship between these numbers and the seed, at first i thought these 624 numbers was just calculated using the MT19937 way, but it's not so:
>>> import random
>>>
>>> class MT19937RNG:
... def __init__(self, seed):
... self.MT = [0] * 624
... self.index = 0
... self.MT[0] = seed & 0xffffffff
... for i in range(1, 623+1):
... self.MT[i] = ((0x6c078965 * (self.MT[i-1] ^ (self.MT[i-1] >> 30))) + i) & 0xffffffff
... def generate_numbers(self):
... for i in range(0, 623+1):
... y = (self.MT[i] & 0x80000000) + (self.MT[(i+1) % 624] & 0x7fffffff)
... self.MT[i] = self.MT[(i + 397) % 624] ^ (y >> 1)
... if (y % 2) != 0:
... self.MT[i] = self.MT[i] ^ (2567483615)
... def extract_number(self):
... if self.index == 0:
... self.generate_numbers()
... y = self.MT[self.index]
... y = y ^ (y >> 11)
... y = y ^ ((y << 7) & (0x9d2c5680))
... y = y ^ ((y << 15) & (0xefc60000))
... y = y ^ (y >> 18)
... self.index = (self.index + 1) % 624
... return y
...
>>> x = MT19937RNG(123456)
>>> random.seed(123456)
>>>
>>> MT1 = x.MT
>>> MT2 = list(random.getstate()[1][:-1])
>>>
>>> MT1==MT2
False
So i wonder why the value of MT1
is different from MT2
, which means why random.getstate()
doesn't using MT19937 to generate its 624 MT numbers, how can we get seed=123456
from MT2
?
Aucun commentaire:
Enregistrer un commentaire