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

Michael P. Reilly arcege@speakeasy.net
Wed, 25 Jul 2001 10:32:28 -0400 (EDT)


alan.gauld@bt.com wrote
> 
> > > 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().

If you are working on an database file or similar construct and cannot put
the values into a sequence object, then choice would not be very useful.
You might be more inclined to use randint.

  -Arcege

-- 
+----------------------------------+-----------------------------------+
| Michael P. Reilly                | arcege@speakeasy.net              |