Skip to content

Instantly share code, notes, and snippets.

@maxattack
Created March 9, 2014 07:15
Show Gist options
  • Save maxattack/9444004 to your computer and use it in GitHub Desktop.
Save maxattack/9444004 to your computer and use it in GitHub Desktop.
Two Envelopes
#!/usr/bin/python
import random
nonSwitcherMoney = 0
switcherMoney = 0
def random_amount_of_money(): return 1 + 999 * random.random()
def coin_flip(): return random.random() > 0.5
# do a million trials
for i in xrange(1000000):
# pick an amount in the smaller envelope, X
X = random_amount_of_money()
# randomly pick an envelope to give to the player
player_received_smaller = coin_flip()
if player_received_smaller:
nonSwitcherMoney += X
switcherMoney += 2.0 * X
else:
nonSwitcherMoney += 2.0 * X
switcherMoney += X
print "Non Switcher Money:", nonSwitcherMoney
print "Always-Switch Money:", switcherMoney
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment