Skip to content

Instantly share code, notes, and snippets.

@davydany
Last active August 29, 2015 14:22
Show Gist options
  • Save davydany/7e1ba8441058c9d24827 to your computer and use it in GitHub Desktop.
Save davydany/7e1ba8441058c9d24827 to your computer and use it in GitHub Desktop.
roulette-sim.py
#!/usr/bin/python
import random
NUMBER_OF_PLAYS = 1000000
class GamblingGame(object):
pass
class Roulette(GamblingGame):
bets = {}
playable_numbers = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'10', '11', '12', '13', '14', '15', '16', '17', '18', '19',
'20', '21', '22', '23', '24', '25', '26', '27', '28', '29',
'30', '31', '32', '33', '34', '35', '36', '00']
def __init__(self, *args, **kwargs):
super(Roulette, self).__init__(*args, **kwargs)
def place_bets(self, **kwargs):
"""
Key: The number to bet on.
Value: The amount to bet.
"""
bets = {}
for k, v in kwargs.items():
key = str(k)
value = v
if key not in self.playable_numbers:
raise ValueError("%s is not a valid number." % k)
bets[key] = value
self.bets.update(bets)
def clear_bets(self):
self.bets = {}
def play(self):
size = len(self.playable_numbers) - 1
pos = random.randint(0, size)
roulette_value = self.playable_numbers[pos]
# deal with winning
if roulette_value in self.bets.keys():
winning = self.bets[roulette_value] * 35
self.clear_bets()
return winning
# deal with loss
net_loss = 0
for k, v in self.bets.items():
net_loss -= v
return net_loss
def play_roulette():
game = Roulette()
# determine bets
# bets = {
# '29': 10,
# '8': 10,
# '12': 10,
# '25': 10,
# '10': 10
# }
bets = {
# reds
'1': 10,
'3': 10,
'5': 10,
'7': 10,
'9': 10,
'12': 10,
'14': 10,
'16': 10,
# blacks
'2': 10,
'4': 10,
'6': 10,
'8': 10,
'10': 10,
'11': 10,
'13': 10,
'15': 10,
'17': 10,
'20': 10,
'22': 10,
'24': 10,
'26': 10,
'28': 10,
'29': 10,
'31': 10,
'33': 10,
'35': 10
}
# play
winnings = 0
amount_bet = 0
for i in xrange(NUMBER_OF_PLAYS):
print "Trial #", i
amount_bet += (5*10)
game.clear_bets()
game.place_bets(**bets)
win = game.play()
# print "Game #%s: $%s; \t Net Winnings: %s" % (i, win, winnings)
winnings += win
# print "Final Winnings: %s" % winnings
# print "Amount Spent: %s" % amount_bet
return winnings, amount_bet
if __name__ == "__main__":
winnings = 0
amount_bet = 0
winnings, amount_bet = play_roulette()
print "The final winnings is: %s" % winnings
print "The final amount spent is: %s" % amount_bet
print "Net Winnings: %s" % (winnings - amount_bet)
percentage = (((winnings - amount_bet) / float(amount_bet))) * 100
print "Percent Win: %s" % percentage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment