Created
February 26, 2017 04:50
-
-
Save RogerZhangHS/f6d8e50acfca8b7461227afd91004661 to your computer and use it in GitHub Desktop.
Validation for the Casino problem
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 | |
import math | |
p = 0.00 | |
trapped = 0 | |
p = float(raw_input("Please enter the value of p (e.g. 0.49): ")) | |
# print "Initializing with with fund: ${}.".format(money) | |
for i in range(1000000): | |
print i | |
investment = 0.00 | |
failure = 0 | |
identical_money = 0 | |
money = 200 | |
while money<500: | |
# print money | |
# print math.log(1-(2*p))/math.log(0.5) | |
last_money = money | |
if identical_money > 10000000: | |
trapped = trapped + 1 | |
print "TRAPPED" | |
break | |
if money<=0: | |
trapped = trapped + 1 | |
print "TRAPPED" | |
break | |
# print failure | |
# if failure <= (math.log(1-(2*p))/math.log(0.5)): | |
if failure <= 2: | |
# print failure | |
investment = 0 | |
else: | |
if money < 1: | |
print "TRAPPED" | |
break | |
elif money == 1: | |
investment = 1 | |
else: | |
investment = money/2 | |
# print investment | |
result = random.random() | |
# print "The result is {}".format(result) | |
if result < 2*p: # IF WIN | |
# print "WOULD WON" | |
money += investment | |
failure = 0 | |
if result > 0.5**(failure+1): # IF LOSE | |
# print "WOULD LOSE" | |
money = money - investment | |
failure += 1 | |
if last_money == money: | |
identical_money += 1 | |
else: | |
identical_money = 0 | |
# print "Investment: {}".format(investment) | |
# print "Money: {}".format(money) | |
print "FAILURE NUM:{}".format(trapped) | |
# print "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment