Skip to content

Instantly share code, notes, and snippets.

@justinvoss
Created February 5, 2011 22:42
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 justinvoss/812878 to your computer and use it in GitHub Desktop.
Save justinvoss/812878 to your computer and use it in GitHub Desktop.
#!/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