Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- class Gambler:
- def __init__(self,startingMoney,costPerTicket,payoff,successProbability):
- self.money = startingMoney
- self.cost = costPerTicket
- self.prob = successProbability
- self.payoff = payoff
- self.plays = 0
- def scratch(self):
- if self.money >= self.cost:
- self.money -= self.cost
- if random.random() <= self.prob:
- self.money += self.payoff
- self.plays += 1
- return True
- else:
- return False
- def canPlay(self):
- if self.money >= self.cost:
- return True
- else:
- return False
- def checkPlayCount(self):
- return self.plays
- def simulate(startingMoney,costPerTicket,payoff,successProbability,minGamesForSuccess, trials = 10**5):
- successes = 0
- games = 0
- while games < trials:
- addict = Gambler(startingMoney,costPerTicket,payoff,successProbability)
- while addict.canPlay() and (addict.checkPlayCount() < minGamesForSuccess):
- addict.scratch()
- if addict.checkPlayCount() == minGamesForSuccess:
- successes += 1
- games += 1
- return successes/games
- print(simulate(100,5,10,.2,30))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement