Skip to content

Instantly share code, notes, and snippets.

@lucrib
Created October 14, 2022 14:45
Show Gist options
  • Save lucrib/dce59c681885ec20cf2d83f50a4fd526 to your computer and use it in GitHub Desktop.
Save lucrib/dce59c681885ec20cf2d83f50a4fd526 to your computer and use it in GitHub Desktop.
A simplistic mega-sena lottery game simulator
import functools
import random
import time
from collections import defaultdict
from threading import Thread
class Timer(Thread):
def __init__(self, interval, func, *args, **kwargs):
super().__init__()
self.interval = interval
self.func = functools.partial(func, *args, **kwargs)
self.should_stop = False
def run(self):
while not self.should_stop:
time.sleep(self.interval)
self.func()
def stop(self):
self.should_stop = True
self.join()
NUMERO_INICIAL = 1
NUMERO_FINAL = 60
TAMANHO_MENOR_JOGO = 6
TAMANHO_MAIOR_JOGO = 6
NUMBERS = range(NUMERO_INICIAL, NUMERO_FINAL + 1)
TIMER = Timer(1, lambda: print(".", end=""))
def get_game(size=TAMANHO_MENOR_JOGO):
return set(random.sample(NUMBERS, size))
sorteio = get_game()
print(f"Sorteio: {sorted(sorteio)}")
report_games_amount = 0
report_games_amount_by_size = defaultdict(int)
st = time.time()
TIMER.start()
while True:
game_size = random.randint(TAMANHO_MENOR_JOGO, TAMANHO_MAIOR_JOGO)
jogo = get_game(game_size)
dezenas_corretas = jogo.intersection(sorteio)
acertos = len(dezenas_corretas)
report_games_amount += 1
report_games_amount_by_size[game_size] += 1
if acertos == 6:
TIMER.stop()
print()
print(f"Habemus ganhador: {sorted(jogo)}, size = {game_size}")
print(f"Quantidade de jogos: {report_games_amount}")
print(f"Quantidade de jogos por tamanho: {report_games_amount_by_size}")
et = time.time()
print(f"Total time: {et - st}")
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment