sample with replacement

For taking k random samples (with replacement) from a population, where k may be greater than len(population).


random.sample() lets you do random sampling without replacement. sample_wr() lets you sample with replacement.

Some simple examples:

tosses = sample_wr(('H', 'T'), 100) # simulate 100 coin tosses rolls = sample_wr(range(1,7), 100) # simulate 100 dice rolls

make a random string from 200 characters over a given alphabet

from string import letters as alphabet rstr = ''.join(sample_wr(alphabet, 200))

NOTE:

You could use

from random import choice sample = [choice(population) for i in xrange(k)]

but that is 2-4 times slower than sample_wr(population, k) for 10E3 <= k <= 10E6

from random import random n = len(population) sample = [population[int(random()*n)] for i in xrange(k)]

is better but still slower.

# credit author(s) of random.py
import random

def sample_wr(population, k):
    "Chooses k random elements (with replacement) from a population"
    n = len(population)
    _random, _int = random.random, int  # speed hack 
    result = [None] * k
    for i in xrange(k):
        j = _int(_random() * n)
        result[i] = population[j]
    return result

code.activestate.com ↗