Last active
December 20, 2015 11:39
-
-
Save vicapow/6124529 to your computer and use it in GitHub Desktop.
randomly pull items out of a list using weights for each item (with replacement)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import * | |
import random | |
w = [0.9,0.1] #faster if the weighted list is pre-sorted, descending | |
p = ['victor', 'powell'] | |
N = len(w) | |
p2 = [] | |
for i in range(N): | |
stop = 0 | |
n = random.random() | |
for j in range(N): | |
stop += w[j] | |
if n < stop: | |
p2.append(p[j]) | |
break | |
print p2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment