Skip to content

Instantly share code, notes, and snippets.

@benmccormick
Created July 27, 2014 17:26
Show Gist options
  • Save benmccormick/d55ff64e5c7e4425cca7 to your computer and use it in GitHub Desktop.
Save benmccormick/d55ff64e5c7e4425cca7 to your computer and use it in GitHub Desktop.
Random Method of selecting 2 contestants from a list
import random
# Implementation of Resevoir Algorithm, see
# http://stackoverflow.com/a/3540315/1424361
def random_line(contestants):
line = next(contestants)
line2 = line
# go through each line, check to see if we
# should select it, with decreasing probability
# over time
for num, aline in enumerate(contestants):
if not random.randrange(num + 2):
line = aline
if not random.randrange(num + 2):
line2 = aline
return line + line2
if __name__ == "__main__":
contestants = open('contestlist.csv','r')
print random_line(contestants)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment