fastest method to choose a random element

caca at mailinator.com caca at mailinator.com
Sat Jan 5 05:16:19 EST 2008


> Caching might help.
>
> If random_pick is called several times with the same list(s) then
> cache the result of
>  [property(i) for i in a_list] against a_list.
>
> If random_pick is called several times with list(s) with multiple
> instances of list items then cache
>  property(i) against i for i in a_list .
>
> You could do both.
>
> You might investigate wether property(i) could be implemented using a
> faster algorithm, maybe table lookup, or interpolation from initial
> table lookup.
>
> - Paddy.

Thanks, Paddy. Those are interesting general comments for the general
problem.
By the way, I noticed two things:

I forgot to write randint in the third approach:

> >     a=globalRNG.randint(1,len(a_list))

The warning "The group you are posting to is a Usenet group. Messages
posted to this group will make your email address visible to anyone on
the Internet." means a person, but not a bot, may see my email
address, so it is safe to use my real address next time...




More information about the Python-list mailing list