Skip to content

Instantly share code, notes, and snippets.

@crmccreary
Last active January 14, 2016 01:36
Show Gist options
  • Save crmccreary/06568a984c4d131cd3b6 to your computer and use it in GitHub Desktop.
Save crmccreary/06568a984c4d131cd3b6 to your computer and use it in GitHub Desktop.
A simple powerball simulator
from numpy.random import choice
JACKPOT = 1.5e9
NUM_TICKETS = 1000
def gen_numbers():
'''
Pick 5 numbers, without replacement, from 1-69
and the powerball from 1-26
To remove the numpy dependency, you can use
import random
numbers = random.sample(list(range(1,70)), 5)
... etc.
'''
numbers = list(choice(list(range(1,70)),size=5,replace=False))
powerball = choice(list(range(1,27)),size=1)[0]
return set(numbers), powerball
def winnings(picked_number_set,
picked_powerball,
drawn_number_set,
drawn_powerball):
'''
Calculate winnings
'''
if len(picked_number_set & drawn_number_set) == 0 and picked_powerball == drawn_powerball:
return 4.0
elif len(picked_number_set & drawn_number_set) == 1 and picked_powerball == drawn_powerball:
return 4.0
elif len(picked_number_set & drawn_number_set) == 2 and picked_powerball == drawn_powerball:
return 7.0
elif len(picked_number_set & drawn_number_set) == 3 and picked_powerball != drawn_powerball:
return 7.0
elif len(picked_number_set & drawn_number_set) == 3 and picked_powerball == drawn_powerball:
return 100.0
elif len(picked_number_set & drawn_number_set) == 4 and picked_powerball != drawn_powerball:
return 100.0
elif len(picked_number_set & drawn_number_set) == 4 and picked_powerball == drawn_powerball:
return 50000.0
elif picked_number_set == drawn_number_set and picked_powerball != drawn_powerball:
return 1.0e6
elif picked_number_set == drawn_number_set and picked_powerball == drawn_powerball:
return JACKPOT
else:
return 0.0
def jackpot(picked_number_set,
picked_powerball,
drawn_number_set,
drawn_powerball):
return picked_number_set == drawn_number_set and picked_powerball == drawn_powerball
def simulation():
picks = []
for i in range(NUM_TICKETS):
picks.append(gen_numbers())
trials = 0
total_winnings = 0.0
while True:
drawn_number_set, drawn_powerball = gen_numbers()
trials = trials + 1
for picked_number_set, picked_powerball in picks:
total_winnings = total_winnings + winnings(picked_number_set,
picked_powerball,
drawn_number_set,
drawn_powerball)
if jackpot(picked_number_set,
picked_powerball,
drawn_number_set,
drawn_powerball):
print('JACKPOT!')
print('Trials: {} Spent: {:,.2f} Total winnings: {:,.2f}'.format(trials,
NUM_TICKETS*trials*2.0,
total_winnings))
return
print('Trials: {} Spent: {:,.2f} Total winnings: {:,.2f}'.format(trials,
NUM_TICKETS*trials*2.0,
total_winnings))
if NUM_TICKETS*trials*2.0 - total_winnings >= JACKPOT:
print('You have spent more than the jackpot and your winnings, why continue?')
return
if trials/104.0 > 25:
print('You have been doing this for 25 years, why continue?')
return
if __name__ == '__main__':
simulation()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment