Author: Antonio Cuni <[email protected]>
Branch: py3k
Changeset: r57299:96980a3ef3af
Date: 2012-09-12 16:45 +0200
http://bitbucket.org/pypy/pypy/changeset/96980a3ef3af/

Log:    fix getrandbits() for the case where rbigint.SUPPORT_INT128 is
        enabled. Previosuly it always returned 0 but the test incorrectly
        passed because it returned a non-normalized rbigint(0, sign=1),
        while the correct form for 0 has sign==0.

diff --git a/pypy/module/_random/interp_random.py 
b/pypy/module/_random/interp_random.py
--- a/pypy/module/_random/interp_random.py
+++ b/pypy/module/_random/interp_random.py
@@ -73,7 +73,7 @@
         w_item = space.getitem(w_state, space.newint(rrandom.N))
         self._rnd.index = space.int_w(w_item)
 
-    #assert rbigint.SHIFT <= 32
+    assert rbigint.SHIFT <= 64
     @unwrap_spec(k=int)
     def getrandbits(self, space, k):
         if k <= 0:
@@ -82,12 +82,17 @@
         needed = (k - 1) // rbigint.SHIFT + 1
         result = rbigint.rbigint([rbigint.NULLDIGIT] * needed, 1)
         for i in range(needed):
+            if rbigint.SHIFT <= 32:
+                value = self._rnd.genrand32()
+            else:
+                value = self._rnd.genrand32() << 32 | self._rnd.genrand32()
             # This wastes some random digits, but not too many since SHIFT=31
-            value = self._rnd.genrand32() & rbigint.MASK
+            value = value & rbigint.MASK
             if i < needed - 1:
                 result.setdigit(i, value)
             else:
                 result.setdigit(i, value >> ((needed * rbigint.SHIFT) - k))
+        result._normalize()
         return space.newlong_from_rbigint(result)
 
 
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to