[Python-bugs-list] [ python-Bugs-594996 ] OverflowError in random.randrange

noreply@sourceforge.net noreply@sourceforge.net
Wed, 14 Aug 2002 09:59:14 -0700


Bugs item #594996, was opened at 2002-08-14 06:46
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=594996&group_id=5470

Category: Python Library
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Shannon Jones (sjones)
>Assigned to: Tim Peters (tim_one)
Summary: OverflowError in random.randrange

Initial Comment:
The following code can produce an OverflowError in
random.randrange.

import random
from sys import maxint
minint = -maxint - 1

random.randrange(minint, maxint)

You may need to execute the last line a few times to
get the error because it depends on the random number
generated. The traceback is on line 315 of random.py
and is OverflowError: float too large to convert.

Changing line 315 from:
return istart + int(self.random() * (istop - istart))
to:
return int(istart + self.random() * (istop - istart))

seems to fix the problem.

-----
I'm using Python 2.3 from CVS on RH 7.3


----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2002-08-14 12:59

Message:
Logged In: YES 
user_id=31435

The suggested change stops OverflowError but delivers 
incorrect results; for example, try randrange(-2, 0) repeatedly, 
and you'll find it delivers one of {-2, -1} before the change, but 
one of {-1, 0} after the change.  Using int(math.floor(...)) 
instead would restore correct behavior, but I'm loathe to slow 
this high-usage function.  In the absence of an idea that's 
both correct and fast, I'll close this as WontFix -- speed is 
more important here than pushing the limit on the overall 
range by 1 bit.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=594996&group_id=5470