Skip to content

Instantly share code, notes, and snippets.

@jzelinskie
Last active January 2, 2016 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jzelinskie/8289498 to your computer and use it in GitHub Desktop.
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).
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