Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Mega-Millions Simulator in Python3
import random
def GetTime(draws):
# Just a helper function for printing how much time has passed to win the jackpot.
years = draws // 104
rem = draws % 104
weeks = rem // 2
return "{} years and {} weeks".format(years, weeks)
# Automatically generates lottery numbers, since there's an equal chance of hitting any particular set,
# selecting your own numbers is irrelevant.
# Gets a unique set of 5 numbers from 1-70
my_numbers = set(random.sample(range(1, 71), 5))
# Set your Mega-Ball (1 - 25) *NOT unique, can be a duplicate number from my_numbers
mega_ball = random.randint(1, 25)
print("Your Quick-Pick lottery numbers: {} + MB: {}".format(my_numbers, mega_ball))
print("Calculating time to win, this could take a while...")
drawings = 0
while True:
drawings += 1
# Checking for mega_ball first, since we avoid a lot of unnecessary comparisons if it doesn't match.
if (random.randint(1, 25) == mega_ball and my_numbers == set(random.sample(range(1, 71), 5))):
print("You won! Drawing number {} is your lucky break!".format(drawings))
print("Your numbers hit the jackpot after playing the lottery for {}".format(GetTime(drawings)))
print("You spent ${} on lottery tickets.".format(drawings * 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment