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: random.randint fails on lists
Type: Stage:
Components: Library (Lib) Versions: Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: quiver, sardonics
Priority: normal Keywords:

Created on 2007-03-07 08:10 by sardonics, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg31456 - (view) Author: sardonics (sardonics) Date: 2007-03-07 08:10
>>> 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
msg31457 - (view) Author: George Yoshida (quiver) (Python committer) Date: 2007-03-07 08:35
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.
History
Date User Action Args
2022-04-11 14:56:23adminsetgithub: 44675
2007-03-07 08:10:18sardonicscreate