Skip to content

Instantly share code, notes, and snippets.

@Diapolo10
Created January 10, 2023 17:07
Show Gist options
  • Save Diapolo10/e7377c147d316510e06b2b013429aac1 to your computer and use it in GitHub Desktop.
Save Diapolo10/e7377c147d316510e06b2b013429aac1 to your computer and use it in GitHub Desktop.
Powerball Lottery Simulator
"""Powerball Lottery Simulator"""
import json
import random
POSSIBLE_WHITE_VALUES = range(1, 70)
POSSIBLE_RED_VALUES = range(1, 27)
WHITE_DRAWS = 5
RED_DRAWS = 1
TICKET_PRICE = 2
TICKETS_PER_DRAWING = 100
DRAWING_COUNT = 15600
Drawing = dict[str, tuple[int, ...]]
def calculate_win_amount(player_numbers: Drawing, winning_numbers: Drawing, times_won: dict[str, int]) -> int:
white_matches = sum(
player in winning_numbers['white']
for player in player_numbers['white']
)
power_matches = sum(
player in winning_numbers['red']
for player in player_numbers['red']
)
match (white_matches, power_matches):
case (5, 1):
win_amount = 2_000_000_000
case (5, 0):
win_amount = 1_000_000
case (4, 1):
win_amount = 50_000
case (4, 0) | (3, 1):
win_amount = 100
case (3, 0) | (2, 1):
win_amount = 7
case (_, 1):
win_amount = 4
case _:
win_amount = 0
wins_key = f"{white_matches}+{'p' if power_matches else ''}"
if wins_key in times_won:
times_won[wins_key] += 1
return win_amount
def main():
total_spent = 0
earnings = 0
times_won = {
"5+P": 0,
"5": 0,
"4+P": 0,
"4": 0,
"3+P": 0,
"3": 0,
"2+P": 0,
"1+P": 0,
"P": 0,
"0": 0,
}
hit_jackpot = False
drawings = 0
years = 0
while not hit_jackpot:
drawings += 1
white_drawing = tuple(sorted(random.sample(POSSIBLE_WHITE_VALUES, k=WHITE_DRAWS)))
red_drawing = tuple(sorted(random.sample(POSSIBLE_RED_VALUES, k=RED_DRAWS)))
winning_numbers: Drawing = {
'white': white_drawing,
'red': red_drawing,
}
for _ in range(TICKETS_PER_DRAWING):
total_spent += TICKET_PRICE
player_numbers: Drawing = {
'white': tuple(sorted(random.sample(POSSIBLE_WHITE_VALUES, k=WHITE_DRAWS))),
'red': tuple(sorted(random.sample(POSSIBLE_RED_VALUES, k=RED_DRAWS))),
}
winnings = calculate_win_amount(player_numbers, winning_numbers, times_won)
if winnings == 2_000_000_000:
hit_jackpot = True
break
if drawings % 156 == 0:
years += 1
print(f"{years} years")
print(f"Spent: ${total_spent}")
print(f"Earnings: ${earnings}")
print(json.dumps(times_won, indent=4))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment