Last active
January 2, 2016 10:19
-
-
Save jzelinskie/8289498 to your computer and use it in GitHub Desktop.
Select a random element from a stream with uniform probability (we only have access to the current element, and a function to get the next element which returns EOF when at the end of the stream).
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 | |
def uniform_random_from_stream(stream): | |
""" Returns a random number from an enumerable with uniform probability | |
by using a "reservoir sampling" algorithm """ | |
reservoir = [] | |
sample_count = 1 | |
for index, item in enumerate(stream): | |
if index < sample_count: | |
reservoir.append(item) | |
else: | |
r = random.randint(0, index) | |
if r < sample_count: | |
reservoir[r] = item | |
return reservoir[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment