This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: possible bug in randint
Type: behavior Stage:
Components: None Versions: Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: cephalo, rhettinger
Priority: normal Keywords:

Created on 2007-12-28 03:58 by cephalo, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (7)
msg59012 - (view) Author: Rich Marinaccio (cephalo) Date: 2007-12-28 03:58
I have been using python for Civilization IV map scripting, and I 
believe I have found a small problem with the randint function. Once in 
a blue moon, randint(a,b) returns b + 1, rather than a >= N <= b. I 
have seen this behavior a few times. Here is the code I used to 
determine this.

randIndex = PWRand.randint(0,len(preshuffle)-1)#PWRand calls randint 
directly with these parameters
if randIndex < 0 or randIndex >= len(preshuffle):
    raise ValueError, " bad index shuffling plot list randIndex=%(r)d 
listLength=%(l)d, mapSize=%(s)d" % {"r":randIndex,"l":len
(preshuffle),"s":hm.mapWidth*hm.mapHeight}

#output is
ValueError:  bad index shuffling plot list randIndex=1453 
listLength=1453, mapSize=13824

according to the docs, the max randIndex should be listLength - 1
msg59014 - (view) Author: Rich Marinaccio (cephalo) Date: 2007-12-28 04:00
I have been using python for Civilization IV map scripting, and I 
believe I have found a small problem with the randint function. Once in 
a blue moon, randint(a,b) returns b + 1, rather than a <= N <= b. I 
have seen this behavior a few times. Here is the code I used to 
determine this.

randIndex = PWRand.randint(0,len(preshuffle)-1)#PWRand calls randint 
directly with these parameters
if randIndex < 0 or randIndex >= len(preshuffle):
    raise ValueError, " bad index shuffling plot list randIndex=%(r)d 
listLength=%(l)d, mapSize=%(s)d" % {"r":randIndex,"l":len
(preshuffle),"s":hm.mapWidth*hm.mapHeight}

#output is
ValueError:  bad index shuffling plot list randIndex=1453 
listLength=1453, mapSize=13824

according to the docs, the max randIndex should be listLength - 1
msg59026 - (view) Author: Rich Marinaccio (cephalo) Date: 2007-12-28 15:36
I've done some more testing and I can't get this to repeat with randint 
alone. It must be an issue with combination of the len() function used 
as a parameter to randint. The above code is in a loop that further 
down will del preshuffle[randIndex]. Could this be a threading issue 
where len(preshuffle) takes time to update when the list is shortened?
msg59029 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2007-12-29 03:58
Threading is the likely source of your problems.  If you concur, please 
close this as invalid.
msg59034 - (view) Author: Rich Marinaccio (cephalo) Date: 2007-12-29 16:16
To be clear, I am not using multi-threading in my particular module. I 
can't explain this behavior with my code alone. The issue is 
complicated by the fact that my module is called by Civ IV, and I have 
no idea what the game is doing behind the scenes. I also don't know 
exactly what happens when 'del myList[index]' is called. You would 
think that len(preshuffle) would have the same value for the randint 
call that it does for the print statement. I have an idea for another 
test I would like to try.

This is my first issue that I've reported in Python. Would it help for 
me to attach the whole file?
msg59035 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2007-12-29 16:56
Attaching the whole file isn't a step in the right direction.  The 
preferred approach is to isolate the problem as tightly as possible.

This report is dubious because, I can't get the following to fail:

from random import seed, randint
seed('mystart')
while 1:
   assert 0 <= randint(0, 1452) <= 1452

There is also no failure with:

preshuffle = [None] * 1453
while 1:
   assert 0 <= randint(0, len(preshuffle)-1) <= 1452

Make sure that PWRand.randint() really does call randint() and not 
randrange() and that it doesn't adjust the range of inputs.
msg59036 - (view) Author: Rich Marinaccio (cephalo) Date: 2007-12-29 17:40
What was happening before was I was getting an index out of range error 
every so often, so then I put in the ValueError catch to see what was 
going on. I was surprised to see that randIndex was the same as len
(preshuffle). I have some further catches in place now that might tell 
me more. It must have to do with deleting items from the list in the 
loop and not getting the right length from len immediately after, but 
then again I use this algorithm in several places in my module with no 
trouble, though with smaller lists. Go ahead and close this, and when I 
have more information I can open it some other time. This bug I'm 
seeing is extremely intermittant.
History
Date User Action Args
2022-04-11 14:56:29adminsetgithub: 46045
2007-12-30 10:41:59rhettingersetstatus: open -> closed
resolution: not a bug
2007-12-29 17:40:46cephalosetmessages: + msg59036
2007-12-29 16:56:41rhettingersetmessages: + msg59035
2007-12-29 16:16:03cephalosetmessages: + msg59034
2007-12-29 03:58:11rhettingersetnosy: + rhettinger
messages: + msg59029
2007-12-28 15:36:33cephalosetmessages: + msg59026
2007-12-28 04:00:00cephalosetmessages: + msg59014
2007-12-28 03:58:44cephalocreate