[Tutor] fast sampling with replacement

Kent Johnson kent37 at tds.net
Sat Feb 20 20:50:06 CET 2010

On Sat, Feb 20, 2010 at 11:22 AM, Andrew Fithian <afith13 at gmail.com> wrote:
>  can
> you help me speed it up even more?
> import random
> def sample_with_replacement(list):
>     l = len(list) # the sample needs to be as long as list
>     r = xrange(l)
>     _random = random.random
>     return [list[int(_random()*l)] for i in r]

You don't have to assign to r, just call xrange() in the list comp.
You can cache int() as you do with random.random()
Did you try random.randint(0, l) instead of int(_random()*i) ?
You shouldn't call your parameter 'list', it hides the builtin list
and makes the code confusing.

You might want to ask this on comp.lang.python, many more optimization
gurus there.


More information about the Tutor mailing list