[pypy-commit] pypy default: (fijal, arigo) improve the random generation a bit
fijal
noreply at buildbot.pypy.org
Fri Oct 24 11:15:33 CEST 2014
Author: Maciej Fijalkowski <fijall at gmail.com>
Branch:
Changeset: r74154:ae4c7b57097a
Date: 2014-10-24 11:15 +0200
http://bitbucket.org/pypy/pypy/changeset/ae4c7b57097a/
Log: (fijal, arigo) improve the random generation a bit
diff --git a/rpython/rlib/rrandom.py b/rpython/rlib/rrandom.py
--- a/rpython/rlib/rrandom.py
+++ b/rpython/rlib/rrandom.py
@@ -4,7 +4,7 @@
# this is stolen from CPython's _randommodule.c
-from rpython.rlib.rarithmetic import r_uint
+from rpython.rlib.rarithmetic import r_uint, intmask
N = 624
M = 397
@@ -72,18 +72,23 @@
i = 1
mt[0] = UPPER_MASK
+ def _conditionally_apply(self, val, y):
+ if y & r_uint(1):
+ return val ^ MATRIX_A
+ return val
+
def genrand32(self):
- mag01 = [0, MATRIX_A]
mt = self.state
if self.index >= N:
for kk in range(N - M):
y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK)
- mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & r_uint(1)]
+ mt[kk] = self._conditionally_apply(mt[kk+M] ^ (y >> 1), y)
for kk in range(N - M, N - 1):
y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK)
- mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & r_uint(1)]
+ mt[kk] = self._conditionally_apply(mt[kk + (M - N)] ^ (y >> 1),
+ y)
y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK)
- mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ mag01[y & r_uint(1)]
+ mt[N - 1] = self._conditionally_apply(mt[M - 1] ^ (y >> 1), y)
self.index = 0
y = mt[self.index]
self.index += 1
@@ -94,8 +99,8 @@
return y
def random(self):
- a = self.genrand32() >> 5
- b = self.genrand32() >> 6
+ a = intmask(self.genrand32() >> 5)
+ b = intmask(self.genrand32() >> 6)
return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0)
def jumpahead(self, n):
More information about the pypy-commit
mailing list