[pypy-commit] pypy disable_merge_different_int_types: (arigo, bivab) fix rrandom and _random

bivab noreply at buildbot.pypy.org
Wed Nov 23 18:17:18 CET 2011


Author: David Schneider <david.schneider at picle.org>
Branch: disable_merge_different_int_types
Changeset: r49699:b52b9ae850fe
Date: 2011-11-23 18:16 +0100
http://bitbucket.org/pypy/pypy/changeset/b52b9ae850fe/

Log:	(arigo, bivab) fix rrandom and _random

diff --git a/pypy/rlib/rrandom.py b/pypy/rlib/rrandom.py
--- a/pypy/rlib/rrandom.py
+++ b/pypy/rlib/rrandom.py
@@ -31,7 +31,7 @@
         mt[0]= s & MASK_32
         for mti in range(1, N):
             mt[mti] = (MAGIC_CONSTANT_A *
-                           (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti)
+                           (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + r_uint(mti))
             # See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier.
             # In the previous versions, MSBs of the seed affect
             # only MSBs of the array mt[].
@@ -52,7 +52,7 @@
         for k in range(max_k, 0, -1):
             mt[i] = ((mt[i] ^
                          ((mt[i - 1] ^ (mt[i - 1] >> 30)) * MAGIC_CONSTANT_C))
-                     + init_key[j] + j) # non linear
+                     + init_key[j] + r_uint(j)) # non linear
             mt[i] &= MASK_32 # for WORDSIZE > 32 machines
             i += 1
             j += 1
@@ -104,5 +104,5 @@
             j = n % i
             mt[i], mt[j] = mt[j], mt[i]
         for i in range(N):
-            mt[i] += i + 1
+            mt[i] += r_uint(i + 1)
         self.index = N
diff --git a/pypy/rlib/test/test_rrandom.py b/pypy/rlib/test/test_rrandom.py
--- a/pypy/rlib/test/test_rrandom.py
+++ b/pypy/rlib/test/test_rrandom.py
@@ -1,4 +1,5 @@
 from pypy.rlib.rrandom import Random, N, r_uint
+from pypy.rlib.rarithmetic import intmask
 import _random
 
 # the numbers were created by using CPython's _randommodule.c
@@ -24,13 +25,13 @@
 
 def test_init_by_array():
     rnd = Random()
-    rnd.init_by_array([1, 2, 3, 4])
+    rnd.init_by_array([r_uint(n) for n in [1, 2, 3, 4]])
     assert rnd.state[:14] == [2147483648, 1269538435, 699006892, 381364451,
             172015551, 3237099449, 3609464087, 2187366456, 654585064,
             2665903765, 3735624613, 1241943673, 2038528247, 3774211972]
     # try arrays of various sizes to test for corner cases
     for size in [N, N - 1, N + 1, N // 2, 2 * N]:
-        rnd.init_by_array(range(N))
+        rnd.init_by_array([r_uint(n) for n in range(N)])
 
 def test_jumpahead():
     rnd = Random()
@@ -47,8 +48,8 @@
     def f(x, y):
         rnd = Random(x)
         rnd.init_by_array([x, y])
-        rnd.jumpahead(y)
+        rnd.jumpahead(intmask(y))
         return rnd.genrand32(), rnd.random()
     t = Translation(f)
-    fc = t.compile_c([int, int])
-    assert fc(1, 2) == f(1, 2)
+    fc = t.compile_c([r_uint, r_uint])
+    assert fc(r_uint(1), r_uint(2)) == f(r_uint(1), r_uint(2))


More information about the pypy-commit mailing list