comparison pytouhou/utils/random.py @ 39:493b503c81e0

Fix PRNG and add some methods to perform like EoSD
author Thibaut Girka <thib@sitedethib.com>
date Mon, 15 Aug 2011 00:38:19 +0200
parents 776453783743
children ab826bc29aa2
comparison
equal deleted inserted replaced
38:cb5b27011044 39:493b503c81e0
14 from time import time 14 from time import time
15 15
16 class Random(object): 16 class Random(object):
17 def __init__(self, seed=None): 17 def __init__(self, seed=None):
18 if seed is None: 18 if seed is None:
19 seed = int(time.time() % 65536) 19 seed = int(time() % 65536)
20 self.seed = seed
20 self.counter = 0 21 self.counter = 0
21 22
22 23
23 def set_seed(self, seed): 24 def set_seed(self, seed):
24 self.seed = seed 25 self.seed = seed
25 self.counter = 0 26 self.counter = 0
26 27
27 28
28 def cycle(self): 29 def rand_uint16(self):
29 # Named this way because the actual return value may be different.
30 # Further reverse engineering might be needed. 30 # Further reverse engineering might be needed.
31 x = ((seed ^ 0x9630) - 0x6553) & 0xffff 31 x = ((self.seed ^ 0x9630) - 0x6553) & 0xffff
32 self.seed = (((x & 0x0c000) >> 0xe) + x*4) & 0xffff 32 self.seed = (((x & 0x0c000) >> 0xe) + x*4) & 0xffff
33 self.counter += 1 33 self.counter += 1
34 self.counter &= 0xffff 34 self.counter &= 0xffff
35 return self.seed 35 return self.seed
36 36
37
38 def rand_uint32(self):
39 # 102h.exe@0x41e7f0
40 a = self.rand_uint16() << 16
41 a |= self.rand_uint16()
42 return a
43
44
45 def rand_double(self):
46 # 102h.exe@0x41e820
47 return float(self.rand_uint32()) / 0x100000000
48