[ python-Bugs-1675516 ] random.randint fails on lists

SourceForge.net noreply at sourceforge.net
Wed Mar 7 09:35:00 CET 2007


Bugs item #1675516, was opened at 2007-03-07 17:10
Message generated for change (Comment added) made by quiver
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1675516&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: Python 2.5
>Status: Closed
>Resolution: Invalid
Priority: 5
Private: No
Submitted By: sardonics (sardonics)
Assigned to: Nobody/Anonymous (nobody)
Summary: random.randint fails on lists

Initial Comment:
>>> for i in range(400):
... 	l[random.randint(0,7)]
... 	
'g'
'g'
'c'
'c'
'b'
'b'
'g'
'g'
'd'
'g'
'd'
'f'
'd'
'a'
Traceback (most recent call last):
  File "<interactive input>", line 2, in ?
IndexError: list index out of range
>>> l
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> 

have no idea what the problem is. check out the error though

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

>Comment By: George Yoshida (quiver)
Date: 2007-03-07 17:35

Message:
Logged In: YES 
user_id=671362
Originator: NO

Please read the doc carefully. randint(a, b) can return b:

http://docs.python.org/lib/module-random.html
> Return a random integer N such that a <= N <= b. 

So in your case, randint(0,7) is returning 7 and tries to access l's 8th
element, which results in IndexError.

Closing as invalid.

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

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


More information about the Python-bugs-list mailing list