INTRODUCTION

Lotto is a Polish lottery that consists in:

  • we buy a ticket for PLN 3, on which we cross out 6 out of 49 numbers (the numbers can also be selected automatically – using the random-hit method)
  • during the official draw broadcast live on TV, 6 numbers out of 49 are also drawn,
  • The degree of winning depends on the number of numbers we hit:
    • 0 hits – lost,
    • 1 hit – loss,
    • 2 hits – loss,
    • 3 hits – fixed win PLN 24,
    • 4 hits – the win depends on the number of other wins in the draw – about a few hundred zlotys,
    • 5 hits – the win depends on the number of other wins in the draw – about a few thousand zlotys,
    • 6 hits – the win depends on the number of other wins in the draw – the lowest guaranteed amount to be divided is PLN 2 million,

We calculate the probability of winning using combinations that allow us to determine the number of k-element subsets in an n-element set.

$C_{n}^{k}= \begin{pmatrix}
n\\
k
\end{pmatrix} $

The number of 6-element subsets from the 49-element set is:

$C_{49}^{6}=
\begin{pmatrix}
49\
6
\end{pmatrix}=\frac{49!}{(49-6)! \cdot 6!} \frac{49\cdot 48\cdot 47\cdot 46\cdot 45\cdot 44\cdot 43!}{43! \cdot 6\cdot 5\cdot 4\cdot 3\cdot 2}=13983816$

TThis means that nearly 14 million different sets of 6 can be drawn from 49 numbers.

This means that the probability of drawing a „six” is:

$\frac{1}{13 983 816} \approx 7,15 \cdot 10^8 \approx 0.000006%$

Simulate Lotto draws.

  • enter six numbers from 1 to 49 in sequence,
  • decide how many draw simulations to run.

PYTHON CODE

import random

# User number input
user_numbers = []
number_names = ["first", "second", "third", "fourth", "fifth", "sixth"]
for i in range(6):
    while True:
        number = int(input(f"Enter {number_names[i]} number from 1-49: "))
        if 1 <= number <= 49:
            if number in user_numbers:
                print(" >>> This number has already been entered, please enter another.")
            else:
                user_numbers.append(number)
                break
        else:
            print(" >>> Invalid number, please enter another.")

# Random number input
number_of_draws = int(input("How many draws do you want to perform? "))

# Drawing and checking for hits
hits = {3: 0, 4: 0, 5: 0, 6: 0}
for _ in range(number_of_draws):
    random_numbers = set(random.sample(range(1, 50), 6))
    hits_count = len(set(user_numbers) & random_numbers)
    if hits_count in hits:
        hits[hits_count] += 1

# Displaying the results
for k, v in hits.items():
    word = "hits" if k != 1 else "hit"
    print(f"Number of {word} {k}: {v}")

⬆️⬆️⬆️ Zobacz w Google Colaboratory


HOW THE CODE WORKS?

  1. we import the library random which allows us to draw numbers.
  2. We create an empty user list that will hold the numbers provided by the user.
  3. Loopfor takes 6 numbers from the user and checks if each is an integer between 1 and 49, then adds it to the user’s list. If the number is incorrect or has already been entered, the program prints an appropriate message and asks again.
  4. The user then enters the number of draws he wants to run.it writes „hit” instead of „hit”.
  5. The program creates a hit dictionary with keys 3 through 6 that represent the number of numbers hit. Each draw consists of generating 6 random numbers between 1 and 49, comparing them with the numbers provided by the user and counting the hits. If 3 to 6 numbers are matched, the program increases the value of the corresponding key in the hit dictionary by 1.
  6. The program displays the number of hits for threes, fours, fives and sixes. If the number of hits is equal to 1, the program prints „hit” and not „hit”.