Debian Codegraph @

rpython/rlib/rrandom.py

raw symbols SCIP
1 # this code is a version of the mersenne twister random number generator which
2 # is supposed to be used from RPython without the Python interpreter wrapping
3 # machinery etc.
4
5 # this is stolen from CPython's _randommodule.c
6
7 from rpython.rlib.rarithmetic import r_uint, intmask
8
9 N = 624
10 M = 397
11 MATRIX_A = r_uint(0x9908b0df) # constant vector a
12 UPPER_MASK = r_uint(0x80000000) # most significant w-r bits
13 LOWER_MASK = r_uint(0x7fffffff) # least significant r bits
14 MASK_32 = r_uint(0xffffffff)
15 TEMPERING_MASK_A = r_uint(0x9d2c5680)
16 TEMPERING_MASK_B = r_uint(0xefc60000)
17 MAGIC_CONSTANT_A = r_uint(1812433253)
18 MAGIC_CONSTANT_B = r_uint(19650218)
19 MAGIC_CONSTANT_C = r_uint(1664525)
20 MAGIC_CONSTANT_D = r_uint(1566083941)
21
22
23 class Random(object):
24 def __init__(self, seed=r_uint(0)):
25 self.state = [r_uint(0)] * N
26 self.index = 0
27 self.init_genrand(seed)
28
29 def init_genrand(self, s):
30 mt = self.state
31 mt[0]= s & MASK_32
32 for mti in range(1, N):
33 mt[mti] = (MAGIC_CONSTANT_A *
34 (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + r_uint(mti))
35 # See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier.
36 # In the previous versions, MSBs of the seed affect
37 # only MSBs of the array mt[].
38 # for >32 bit machines
39 mt[mti] &= MASK_32
40 self.index = N
41
42 def init_by_array(self, init_key):
43 key_length = len(init_key)
44 mt = self.state
45 self.init_genrand(MAGIC_CONSTANT_B)
46 i = 1
47 j = 0
48 if N > key_length:
49 max_k = N
50 else:
51 max_k = key_length
52 for k in range(max_k, 0, -1):
53 mt[i] = ((mt[i] ^
54 ((mt[i - 1] ^ (mt[i - 1] >> 30)) * MAGIC_CONSTANT_C))
55 + init_key[j] + r_uint(j)) # non linear
56 mt[i] &= MASK_32 # for WORDSIZE > 32 machines
57 i += 1
58 j += 1
59 if i >= N:
60 mt[0] = mt[N - 1]
61 i = 1
62 if j >= key_length:
63 j = 0
64 for k in range(N - 1, 0, -1):
65 mt[i] = ((mt[i] ^
66 ((mt[i - 1] ^ (mt[i - 1] >> 30)) * MAGIC_CONSTANT_D))
67 - i) # non linear
68 mt[i] &= MASK_32 # for WORDSIZE > 32 machines
69 i += 1
70 if (i>=N):
71 mt[0] = mt[N - 1]
72 i = 1
73 mt[0] = UPPER_MASK
74
75 def _conditionally_apply(self, val, y):
76 if y & r_uint(1):
77 return val ^ MATRIX_A
78 return val
79
80 def genrand32(self):
81 mt = self.state
82 if self.index >= N:
83 for kk in range(N - M):
84 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK)
85 mt[kk] = self._conditionally_apply(mt[kk+M] ^ (y >> 1), y)
86 for kk in range(N - M, N - 1):
87 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK)
88 mt[kk] = self._conditionally_apply(mt[kk + (M - N)] ^ (y >> 1),
89 y)
90 y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK)
91 mt[N - 1] = self._conditionally_apply(mt[M - 1] ^ (y >> 1), y)
92 self.index = 0
93 y = mt[self.index]
94 self.index += 1
95 y ^= y >> 11
96 y ^= (y << 7) & TEMPERING_MASK_A
97 y ^= (y << 15) & TEMPERING_MASK_B
98 y ^= (y >> 18)
99 return y
100
101 def random(self):
102 a = intmask(self.genrand32() >> 5)
103 b = intmask(self.genrand32() >> 6)
104 return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0)
105
106 def jumpahead(self, n):
107 mt = self.state
108 for i in range(N - 1, 1, -1):
109 j = n % i
110 mt[i], mt[j] = mt[j], mt[i]
111 nonzero = False
112 for i in range(1, N):
113 mt[i] += r_uint(i + 1)
114 mt[i] &= r_uint(0xffffffff)
115 nonzero |= bool(mt[i])
116 # Ensure the state is nonzero: in the unlikely event that mt[1] through
117 # mt[N-1] are all zero, set the MSB of mt[0] (see issue #14591). In the
118 # normal case, we fall back to the pre-issue 14591 behaviour for mt[0].
119 if nonzero:
120 mt[0] += r_uint(1)
121 mt[0] &= r_uint(0xffffffff)
122 else:
123 mt[0] = r_uint(0x80000000)
124 self.index = N