Skip to content

Instantly share code, notes, and snippets.

@cgearhart
Created July 3, 2018 21:49
Show Gist options
  • Save cgearhart/60ff130a181e2835175ee579fbd8bc3b to your computer and use it in GitHub Desktop.
Save cgearhart/60ff130a181e2835175ee579fbd8bc3b to your computer and use it in GitHub Desktop.
Resampling wheel (weighted random sampling) in python
def sample(weights, size=1):
beta = 0
samples = []
w_max = max(weights)
index = random.randint(1, N)
while len(samples) < size:
beta += random.uniform(0, 2 * w_max)
while w[index] < beta:
beta -= weights[index]
index += 1
samples.append(index)
return samples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment