[ python-Bugs-1590891 ] random.randrange don't return correct value for big number

SourceForge.net noreply at sourceforge.net
Wed Nov 8 17:45:36 CET 2006


Bugs item #1590891, was opened at 2006-11-05 08:54
Message generated for change (Comment added) made by josiahcarlson
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1590891&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: None
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: MATSUI Tetsushi (mft)
Assigned to: Raymond Hettinger (rhettinger)
Summary: random.randrange don't return correct value for big number

Initial Comment:
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.

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

Comment By: Josiah Carlson (josiahcarlson)
Date: 2006-11-08 08:45

Message:
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.

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

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


More information about the Python-bugs-list mailing list