]> granicus.if.org Git - python/commitdiff
Bug #1590891: random.randrange don't return correct value for big number
authorRaymond Hettinger <python@rcn.com>
Wed, 20 Dec 2006 06:42:06 +0000 (06:42 +0000)
committerRaymond Hettinger <python@rcn.com>
Wed, 20 Dec 2006 06:42:06 +0000 (06:42 +0000)
Needs to be backported.

Lib/random.py
Lib/test/test_random.py
Misc/NEWS

index ae2d434b3111782690cb2c7b95d7334a0d8c3d87..b80f1a1c3f08433cef60a06ed08d91bd23921843 100644 (file)
@@ -205,7 +205,7 @@ class Random(_random.Random):
             raise ValueError, "empty range for randrange()"
 
         if n >= maxwidth:
-            return istart + self._randbelow(n)
+            return istart + istep*self._randbelow(n)
         return istart + istep*int(self.random() * n)
 
     def randint(self, a, b):
index e3f05a096886da7206ed87bdb6c44c231a6b9ab5..ddbcc2fd323c0a18168b7412528c02b62e237a51 100644 (file)
@@ -438,6 +438,14 @@ class MersenneTwister_TestBasicOps(TestBasicOps):
             self.assertEqual(k, numbits)        # note the stronger assertion
             self.assert_(2**k > n > 2**(k-1))   # note the stronger assertion
 
+    def test_randrange_bug_1590891(self):
+        start = 1000000000000
+        stop = -100000000000000000000
+        step = -200
+        x = self.gen.randrange(start, stop, step)
+        self.assert_(stop < x <= start)
+        self.assertEqual((x+stop)%step, 0)
+
 _gammacoeff = (0.9999999999995183, 676.5203681218835, -1259.139216722289,
               771.3234287757674,  -176.6150291498386, 12.50734324009056,
               -0.1385710331296526, 0.9934937113930748e-05, 0.1659470187408462e-06)
index a4eb6e2261c25b637abb0a4cf0ad9a5fefd316b6..c1b9a12e091ee570fdd585e94fce8f6457d77afd 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 2.6 alpha 1?
 Core and builtins
 -----------------
 
+- Bug #1590891: random.randrange don't return correct value for big number
+
 - Patch #1586791: Better exception messages for some operations on strings,
   tuples and lists.