Skip to content

Instantly share code, notes, and snippets.

@daleobrien
Created October 1, 2012 03:23
Show Gist options
  • Save daleobrien/3809309 to your computer and use it in GitHub Desktop.
Save daleobrien/3809309 to your computer and use it in GitHub Desktop.
Weight Randon Choice
# from http://glowingpython.blogspot.com.au/2012/09/weighted-random-choice.html
from numpy import cumsum, sort, sum, searchsorted
from numpy.random import rand
from pylab import hist,show,xticks
def weighted_pick(weights,n_picks):
"""
Weighted random selection
returns n_picks random indexes.
the chance to pick the index i
is give by the weight weights[i].
"""
t = cumsum(weights)
s = sum(weights)
return searchsorted(t,rand(n_picks)*s)
# weights, don't have to sum up to one
w = [0.1, 0.2, 0.5, 0.5, 1.0, 1.1, 2.0]
# picking 10000 times
picked_list = weighted_pick(w,10000)
# plotting the histogram
hist(picked_list,bins=len(w),normed=1,alpha=.8,color='red')
show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment