Created
July 27, 2014 17:26
-
-
Save benmccormick/d55ff64e5c7e4425cca7 to your computer and use it in GitHub Desktop.
Random Method of selecting 2 contestants from a list
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 | |
# 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