Fixed our lack of raising IndexError on random.choice applied to sequences.

This commit is contained in:
Jeremy Fincher 2004-10-10 03:39:59 +00:00
parent 9363489e32
commit 3a408f3dd9
1 changed files with 4 additions and 1 deletions

View File

@ -53,11 +53,14 @@ def choice(iterable):
return _choice(iterable)
else:
n = 1
ret = None
m = new.module('') # Guaranteed unique value.
ret = m
for x in iterable:
if random.random() < 1/n:
ret = x
n += 1
if ret is m:
raise IndexError
return ret
random.choice = choice