Created
December 4, 2019 18:29
-
-
Save jbn/0e845461f3e5cfdac2847fc6b51ad4ef to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import random | |
def sample(freqs_or_probs, precomputed_max_over_freqs_or_probs=None): | |
x = freqs_or_probs | |
n = len(x) | |
max_value = precomputed_max_over_freqs_or_probs or max(x) | |
while True: | |
i = int(n * random.random()) | |
if random.random() < x[i] / max_value: | |
return i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment