Testing mega sena games

3

My teacher asked me to generate a mega sena generator. I did, and modesty part was pretty cool:

from time import sleep
from random import sample
palpite = list()
a = []
print('='*40)
print('=========PALPITES DA MEGA SENA==========')
print('='*40)
print()
sleep(0.1)
c = []
cont = 0
for i in range(1,61):
    c.append(i)
pergunta = int(input('3[35mQuantos palpites deseja processar? '))
for j in range(0, pergunta):
    palpite.append(sample(c,6))

print()
palpite.sort()
sleep(1)
print('=-'*30)
print('#'*35)
print(f'sortendo {pergunta} numeros')
print('#'*35)
print('=-'*30)
print()
sleep(1.5)
for d in palpite:
    d.sort()
    cont += 1
    sleep(1)
    print(f'{cont}º Jogo = {d}')
sleep(1.5)
print()
print('-='*30)
print('3[1;32m         >>>>> BOA SORTE <<<<<<   ')
print('Fim')
print('3[31m^'*45)

But then a question came to me: I am a player until I am assiduous on the mega seine (at least once a week I make a bet). Is it really possible to win? Then I created a program to test:

from random import sample
numero1 = []
numero2 = []
jogo1 = []
jogo2 = []
for n1 in range(1,61):
    numero1.append(n1)
for n2 in range(1,61):
    numero2.append(n2)
while True:
    jogo1.append(sample(numero1,6))
    jogo2.append(sample(numero2,6))
    for m in jogo1:
        m.sort()
    for z in jogo2:
        z.sort()
    if m == z:
        print(m)
        print(z)
        break
print('fim')

But it did not. There was no error, it seems to have been running, but I'm not sure. I waited about 5 minutes, and nothing. Is it really impossible, or is my program just wrong?

I'm using PyCharm.

    
asked by anonymous 22.08.2018 / 04:45

1 answer

5

There are 50,063,860 chances for results in this game of 6 numbers. The chance of getting an expected [a1,a2,a3,a4,a5,a6] fault is 1/50.063.860 = 0,00000002 which corresponds to 0,000002% . That is, every 50,063,860 games, you are expected to win 1.

I've modified your test code a bit (saving all the values drawn and traversing all with a for .sort () does not seem like a good idea):

from random import sample
import time

numero1 = []
for n1 in range(1,61):
    numero1.append(n1)

tentativas = 0
start = time.time()
while True:
    tentativas += 1
    if tentativas % 500000 == 0:
        print('Tentativas:', tentativas, end = ' | ')
        print('Tempo:', time.time()-start)
    m = sample(numero1, k=6)
    m.sort()
    z = sample(numero1, k=6)
    z.sort()
    if m == z:
        print('Tentativas:', tentativas)
        print('Tempo:', time.time()-start)
        print(m)
        print(z)
        break
print('fim')

I circled it a few times and got the results:

Sample 1:

...
Tentativas: 89500000 | Tempo: 1628.135265827179
Tentativas: 90000000 | Tempo: 1637.143269777298
Tentativas: 90500000 | Tempo: 1645.9052894115448
KeyboardInterrupt

Sample 2:

...
Tentativas: 29500000 | Tempo: 546.6875867843628
Tentativas: 30000000 | Tempo: 556.0576255321503
Tentativas: 30500000 | Tempo: 565.2926483154297
KeyboardInterrupt

Sample 3:

Tentativas: 500000 | Tempo: 9.574995756149292
Tentativas: 1000000 | Tempo: 18.5190167427063
Tentativas: 1009879
Tempo: 18.727997303009033
[10, 13, 28, 37, 49, 56]
[10, 13, 28, 37, 49, 56]
fim

Now, when I change z to z = [1,2,3,4,5,6] :

Sample 1:

...
Tentativas: 10000000 | Tempo: 94.66205024719238
Tentativas: 10500000 | Tempo: 99.3870267868042
Tentativas: 10634304
Tempo: 100.61703157424927
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
fim

Sample 2:

...
Tentativas: 40000000 | Tempo: 381.7722237110138
Tentativas: 40500000 | Tempo: 386.4262237548828
Tentativas: 40534342
Tempo: 386.7422297000885
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
fim

Moral of the story: your code is not wrong and it is not impossible to win, assuming the game is random.

    
22.08.2018 / 15:49