Last active
February 11, 2018 17:00
-
-
Save DavidRdgz/5a08eab79c0224a170bb2138cb822b79 to your computer and use it in GitHub Desktop.
Sampling an infinite stream with reservoir sampling
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
#!/usr/bin/python | |
import random | |
shuf = random.sample(range(100000), 1000) | |
# Keep bag of k=10 elements | |
# Prefill the bag with first shuf items | |
bag = shuf[:10] | |
for i in shuf[10:]: | |
k = random.randint(0, 9) | |
# Update bag in 1/k chance | |
if k == 1: | |
# Randomly kick out old bag item | |
indx = random.randint(0, 9) | |
bag[indx] = i | |
print bag |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment