Advertisement
Guest User

Untitled

a guest
Dec 16th, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. import random
  2.  
  3. class Gambler:
  4. def __init__(self,startingMoney,costPerTicket,payoff,successProbability):
  5. self.money = startingMoney
  6. self.cost = costPerTicket
  7. self.prob = successProbability
  8. self.payoff = payoff
  9. self.plays = 0
  10. def scratch(self):
  11. if self.money >= self.cost:
  12. self.money -= self.cost
  13. if random.random() <= self.prob:
  14. self.money += self.payoff
  15. self.plays += 1
  16. return True
  17. else:
  18. return False
  19. def canPlay(self):
  20. if self.money >= self.cost:
  21. return True
  22. else:
  23. return False
  24. def checkPlayCount(self):
  25. return self.plays
  26.  
  27. def simulate(startingMoney,costPerTicket,payoff,successProbability,minGamesForSuccess, trials = 10**5):
  28. successes = 0
  29. games = 0
  30. while games < trials:
  31. addict = Gambler(startingMoney,costPerTicket,payoff,successProbability)
  32. while addict.canPlay() and (addict.checkPlayCount() < minGamesForSuccess):
  33. addict.scratch()
  34. if addict.checkPlayCount() == minGamesForSuccess:
  35. successes += 1
  36. games += 1
  37. return successes/games
  38.  
  39. print(simulate(100,5,10,.2,30))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement