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

noreply@sourceforge.net noreply@sourceforge.net
Thu, 15 Aug 2002 00:17:32 -0700


Bugs item #594996, was opened at 2002-08-14 05: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: Shannon Jones (sjones)
Date: 2002-08-15 02:17

Message:
Logged In: YES 
user_id=589306

I'm attaching a patch that I *hope* generates correct output
for all inputs :-)

Just for kicks, I tried running random.randrange(-10, 10)
fifty million times with the regular code and with these
changes. The regular version ran in 543.04 seconds. The
changed code took 559.21 seconds. The difference is 2.98%.

To put that in perspective: if you had a simulation that did
nothing but generate random numbers and throw them away for
10 hours, it would now take 10 hours and 18 minutes to run.

If the range between numbers is bigger than sys.maxint,
things slow down by roughly a factor of 5 on my computer. I
guess because Python switches to long ints?


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

Comment By: Tim Peters (tim_one)
Date: 2002-08-14 11: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