selecting a random item from a set
Paul Rubin
http
Tue Dec 30 18:36:51 EST 2003
Paul Rubin <http://phr.cx@NOSPAM.invalid> writes:
> The classic way to do it goes something like this:
>
> for n, randomElement in enumerate(s):
> if random() < (1.0 / (n+1)):
> e = randomElement
> # possibly followed by
> s.remove(randomElement)
Oops, meant to say:
for n, e in enumerate(s):
if random() < (1.0 / (n+1)):
randomElement = e
# possibly followed by
s.remove(randomElement)
More information about the Python-list
mailing list