[Tutor] How to pull a random element from a list?

alan.gauld@bt.com alan.gauld@bt.com
Wed, 25 Jul 2001 15:04:11 +0100


> > The essence is to generate a random integer between 
> > 0 and len(list)-1
> > 
> > randint() is your friend.
>
> Isn't it easier to use random.choice()? Are there any
> advantages to this method i'm missing?

Nope, its just that choice() is a recent addition and I keep 
forgetting it's there! randint() was the best alternative
prior to choice().

In fact in the object framework I use random() and scale 
it manually just to show the technique, since many 
languages only have a randmizer that outputs 0-1 values.

Alan G