Created
November 15, 2012 16:54
-
-
Save ryanwitt/4079732 to your computer and use it in GitHub Desktop.
Can you create an unbiased sample of size k from a large stream in constant memory?
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
import random | |
import matplotlib.pyplot as plt | |
k = 1000 | |
array = [] | |
for n, x in enumerate([range(k)[random.randrange(k)] for x in range(100000)]): | |
if n < k: | |
array.append(x) | |
else: | |
if random.random() < k/float(n): | |
array[random.randrange(k)] = x | |
plt.hist(array) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment