Stylize
Stylize

Reputation: 1098

Using python pick a random element from a list with replacement

I have a list of 40 elements. I am trying to estimate how many times I need to sample this list in order to reproduce all elements in that list. However, it is important that I replace the picked element. I.e. it is possible that I will pick the same element 20 times. So far I have the following

import random
l = range(0,40)

seen=[]
x=0
while len(seen)<len(l):
    r = random.choice(l)
    if r not in seen:
        seen.append(r)
        x=x+1
print x

However, this always returns that it took 40 times to accomplish what I want. However, this is because a single element is never selected twice. Eventually I would run this function 1000 times to get a feel for how often I would have to sample. as always, thanks

Upvotes: 1

Views: 1473

Answers (2)

wenzul
wenzul

Reputation: 4058

You need just adjust the indentation of x=x+1. Because right now you just increment if the value was not seen before.

If you will do that more often with a lot of items may use a set as your seen variable because access items is faster in avarage.

l = range(0, 40)
seen = set()
x = 0
while len(seen) < len(l):
    r = random.choice(l)
    if r not in seen:
        seen.add(r)
    x = x + 1
print x

Upvotes: 3

Cory Kramer
Cory Kramer

Reputation: 118021

Here is a similar method to do it. Initialize a set, which by definition may only contain unique elements (no duplicates). Then keep using random.choice() to choose an element from your list. You can compare your set to the original list, and until they are the same size, you don't have every element. Keep a counter to see how many random choices it takes.

import random

def sampleValues(l):
    counter = 0
    values = set()
    while len(values) < len(l):
        values.add(random.choice(l))
        counter += 1
    return counter

>>> l = list(range(40))

This number will vary, you could Monte Carlo to get some stats

>>> sampleValues(l)
180
>>> sampleValues(l)
334
>>> sampleValues(l)
179

Upvotes: 1

Related Questions