Created
April 18, 2014 17:17
-
-
Save dylanroy/11054825 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
import random | |
NUMBER_OF_PLAYERS = 5 | |
NUMBER_OF_ITERATIONS = 1000000 | |
def main(): | |
if NUMBER_OF_PLAYERS < 2: | |
raise ValueError("Must have at least 2 people.") | |
if NUMBER_OF_ITERATIONS < 1: | |
raise ValueError("Must have at least 1 iteration.") | |
# Initialize the dictionary of players and losses | |
losses = [] | |
for player in range(0, NUMBER_OF_PLAYERS): | |
losses.append(0) | |
# Begin iteration of drawing straws | |
for iteration in range(0, NUMBER_OF_ITERATIONS): | |
# Initialize the dictionary of straws | |
straws = [] | |
straws.append(1) | |
for straw in range(1, NUMBER_OF_PLAYERS): | |
straws.append(0) | |
# Randomize the straws and have each player draw one | |
random.shuffle(straws) | |
for player in range(0, NUMBER_OF_PLAYERS): | |
straw = straws.pop() | |
# If they picked the shortest straw, move to the next round | |
if straw: | |
losses[player] += 1 | |
break | |
# Print results | |
for player in range(0, NUMBER_OF_PLAYERS): | |
print "Player %i lost %i times out of %i rounds (%.2f%%)." % (player + 1, losses[player], NUMBER_OF_ITERATIONS, (float(losses[player]) / float(NUMBER_OF_ITERATIONS)) * 100) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment