classification
Title: random.randrange don't return correct value for big number
Type: Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: arigo, josiahcarlson, mft, rhettinger
Priority: normal Keywords:

Created on 2006-11-05 16:54 by mft, last changed 2006-12-20 06:43 by rhettinger. This issue is now closed.

Files
File name Uploaded Description Edit
randrange1.diff arigo, 2006-11-08 22:22 Proposed fix
Messages (4)
msg30457 - (view) Author: MATSUI Tetsushi (mft) Date: 2006-11-05 16:54
Python 2.4.3 (#1, Oct  3 2006, 00:36:06)
[GCC 4.1.1 (Gentoo 4.1.1-r1)] on linux2
Type "help", "copyright", "credits" or "license" for
more information.
>>> import random
>>> random.randrange(1000000000000,
-100000000000000000000, -200)
267471051174796896L

Obviously, the result is not in the specified range;
1000000000000 < 267471051174796896,
-100000000000000000000 < 267471051174796896
and
(267471051174796896 - 1000000000000) % (-200) != 0.

I'm using 2.3.5 and 2.4.3, and their behaviors are
identical.
I haven't checked about 2.5.
msg30458 - (view) Author: Josiah Carlson (josiahcarlson) * Date: 2006-11-08 16:45
Logged In: YES 
user_id=341410

2.5 has the same behavior.

One workaround (until it gets fixed) is to do the following...

def myrandrange(start, stop, step):
    return start + random.randrange((stop-start)//step)*step

random.randrange should change to do some variant of the
above, given sane start, stop, step arguments.
msg30459 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2006-11-08 22:22
Logged In: YES 
user_id=4771

Oups.  If the interval is very large, the step is
ignored.  Patch attached...
msg30460 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2006-12-20 06:43
Thanks for the report.

Fixed in revision 53099.
History
Date User Action Args
2006-11-05 16:54:05mftcreate