Created
February 5, 2011 22:42
-
-
Save justinvoss/812878 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python | |
# This is the code used to select the winner of | |
# the Tauntaun Sleeping Bag Contest, sponsored by | |
# Bleeding Wolf Productions. | |
# | |
# The code has been made public so it's fairness | |
# can be challenged if necessary. The contestants' | |
# data have been removed to protect their privacy. | |
# | |
# Inspection of this program and the final drawing | |
# was made by a neutral third party. | |
import random | |
contestants = ( | |
('Contestant A', 50), | |
('Contestant B', 10), | |
('Contestant C', 1), | |
) | |
total_score = sum([x for _, x in contestants]) | |
# http://docs.python.org/library/random.html#random.randrange | |
random_drawing = random.randrange(0, total_score, 1) | |
print "Total Scores: ", total_score | |
print "Random drawing: ", random_drawing | |
offset = 0 | |
for name, weight in contestants: | |
r = range(offset, offset+weight) | |
assert len(r) == weight | |
if random_drawing in r: | |
print "" | |
print "---> The winner is " + name | |
print "" | |
break | |
offset += weight |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment