the probability of winning a lottery
  1. Probability
  2. Factorial
  3. Combinations
  4. Calculating the Probability of Winning a Lottery with Python
  5. Creating a GUI 

Have you ever dreamt of being rich? What would you buy if you had millions of rupees? And what if you could have millions of rupees by just spending under 50 rupees? Seems like a dream but there is a possibility it happening. If you are thinking about a lottery, you are absolutely right.

For those who are not familiar with it, a lottery is a gambling game that is used to raise money. At its most basic level, a lottery involves paying a small amount of money to purchase a lottery ticket, and then if we are lucky, our ticket matches the winning ticket and we win a prize, such as a large sum of money.

But what is the probability of you winning a lottery? Maybe not much if you buy one, but what if you buy multiple tickets? Does that increase your chances of winning and if yes, by how much money? Today we are going to find it out by using some basic mathematics and Python programming language. So let us go through some important concepts before knowing the odds of winning a lottery, more specifically a popular lottery contest in India known as lotto-India.


Probability is the branch of mathematics that gives us a numerical description of how likely an event is to occur. Its value is a number between 0 and 1, where, roughly speaking, 0 indicates impossibility and 1 indicates certainty.

We are going to use this branch of mathematics to find out how close we are to become a millionaire. We are going to cover some very basics of probability here. The possibility of an event to happen is equal to the ratio of the number of favourable outcomes and the total number of outcomes.

Probability of event to happen P(E) = Number of favourable outcomes/Total Number of outcomes.

For example, the probability of getting a head when tossing a coin=Total number of heads(favourable outcome here) in coin / total number of possible outcomes. Since in a coin, there is only one head, and only two possible outcomes,viz-head and tail. Putting these numbers in the above formula we get ½ i.e. 0.5 or 50%, which means that we have a 50% chance of getting a head.


The factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 6 is 1*2*3*4*5*6 = 720. Factorial is not defined for negative numbers, and the factorial of zero is one, 0! = 1. Here is a simple program written in python that can calculate the factorial of any number

def factorial(n):
    return fact


A combination is a mathematical technique that gives us the number of possible arrangements that can be made out of a  collection of items. Note that the order of the selection does not matter and you can select the items in any order.

The formula for a combination is C(n,r) = n! / (r! (n-r)!), 

where n represents the number of items and r represents the number of items being chosen at a time.

The python program to calculate the combination is given below:

def combinations(n,k):
    num = factorial(n)
    den = factorial(k) * factorial(n-k)
    return num / den

The output is 3, which means for a list containing 3 elements if we take two items at a time, a total of three combinations are possible. For example for numbers 1,2, and 3 we can two number combinations of (1,2),(1,3), and (2,3) 

Calculating the Probability of Winning a Lottery with Python

probability of winning the lottery

As mentioned above, we are going to calculate the probability of winning lotto India which is a popular lottery contest in India. So let me quickly go through the rules of this lottery. We have to select six numbers from 1 to 50 on one of the game panels, then select a Joker Ball from 1 to 5. For winning the jackpot prize of around 5 crore rupees, all six numbers and the joker ball must match the winning numbers and Joker Ball number. This image completely describes the winning prizes.

So from the above image, it is clear that we have to always aim to get all the numbers as well as the Joker Ball right. Here is a program that gives the odds of you getting any of those prizes.

We chose Python to code this because of its simplicity. Python is considered one of the easiest languages and is very user friendly. It has a simple syntax which makes it ideal for new programmers. Now let us move on to the code. The program is completely dynamic which means that it can work for other lottery competitions also which work on the same rules such as Powerball. You can customize various variables such as:

  • Range: You can specify a different range to select numbers such as 1 to 69, which is in case of Powerball lottery.
  • Number of choices: You can also change the number of numbers you can select, such as you can select it as 5 as in Powerball.
  • Number of joker balls: In Powerball, there are a set of red balls which are similar to joker balls. They are 26 in number
  • Count of matched numbers: Here you can specify the total number of our selections that match the original set of numbers. For example , if we want to know the odds and probability of matching two numbers, we specify this number as 2.
  • Joker ball presence: This variable is True if we match the winning Joker ball or else it is set false. For example, if we want to know the odds and probability of matching two numbers and getting the joker ball number correct, we specify this as True.
  • Tickets: Number of Tickets Bought

def factorial(n):
    return fact

def combinations(n,k):
    num = factorial(n)
    den = factorial(k) * factorial(n-k)
    return num / den

def comb_ways(a,total_num,num_choices):
    return num/den

def one_ticket_probability(total_num,tickets,num_choices,num_joker,match_num,joker_ball=False):
    outcome_numbers=combinations(total_num, num_choices)
    successful_outcome = tickets
    if joker_ball==True:
        outcome_jokerBall = num_joker
    else :
        outcome_jokerBall = (num_joker)/(num_joker-1)
    total_outcomes = (outcome_numbers / comb_ways(match_num, total_num, num_choices)) * outcome_jokerBall
    probability_winning = (successful_outcome / total_outcomes)
    print("You're chances of winning are {:.18f}".format(probability_winning))
    print("You're chances of winning are 1 in {}".format(
    return probability_winning, (total_outcomes / successful_outcome)

number_choices=6 #Number of choices
total_jokerBalls=5 #Number of joker balls
match_numbers=6 #Countr of matched numbers
Joker_present=True #Presence of joker ball
tickets=1 #Total number of tickets bought

for match_numbers in range(match_numbers+1):
        print("Joker Ball matches-",end=" ")
        print("Joker Ball does not match-",end=" ")
    print("Count of Numbers that matched the winning numbers = {}".format(match_numbers))


Creating a GUI for the program

For the program above, we are going to design a basic User Interface using Tkinter. Here is the code which is an addition to the code in the last section.

#First we define a new function that calculates
#the probabilities and also gives results in a message box

import tkinter as tk
from tkinter import messagebox

def Calculate(entries):
    cal = one_ticket_probability(int(entries['Total Number'].get()),
                                 int(entries['Tickets bought'].get()),
                                 int(entries['Choices given'].get()),
                                 int(entries['Match balls'].get()), v.get())
        "For the selected choices ",
        "\nYou're chances of winning are {:.18f} \nYou're chances of winning are 1 in {}\n"
        .format(cal[0], cal[1]))

#We use tkinter to make a window object
root = tk.Tk()
#we make a form for user to give his values
def makeform(root, fields):
    entries = {}
    #These are default values for lotto-india
    for field in fields:
        row = tk.Frame(root)
        lab = tk.Label(row, width=22, text=field + ": ", anchor='w')
        ent = tk.Entry(row)
        row.pack(side=tk.TOP, fill=tk.X, padx=5, pady=5)
        ent.pack(side=tk.RIGHT, expand=tk.YES, fill=tk.X)
        entries[field] = ent
    return entries

fields = [
    'Total Number', 'Choices given', 'total_jokerballs', 'Match balls',
     'Tickets bought'
ents = makeform(root, fields)
v = tk.IntVar()

         text="Jokeball matches",
tk.Radiobutton(root, text="Matches", padx=20, variable=v,
tk.Radiobutton(root, text="Not matches", padx=20, variable=v,
b1 = tk.Button(root, text='Calculate Odds',
           command=(lambda e=ents: Calculate(e)))
b1.pack(side=tk.LEFT, padx=5, pady=5)
b3 = tk.Button(root, text='Quit', command=root.quit)
b3.pack(side=tk.LEFT, padx=5, pady=5)

text2 = tk.Text(root, height=20, width=60)
scroll = tk.Scrollbar(root, command=text2.yview)
text2.tag_configure('bold_italics', font=('Arial', 12, 'bold', 'italic'))
text2.tag_configure('big', font=('Verdana', 20, 'bold'))
                    font=('Tempus Sans ITC', 12, 'bold'))
text2.insert(tk.END, '\nLottery Prediction\n', 'big')
quote = """
Total Number : Totals numbers in range from which numbers are to be chosen e.g. for range 1-49, Total number = 50 
Choices given : Number of numbers we can select excluding the joker ball
Total Jokerballs : Total number of jokerballs from which one ball is to be chosen
Match balls: For how many matches you want to calculate probability
Tickets : Number of Tickets bought
Jokerball matches : Keep True if you want to calculate the probability of jokerball matching too
text2.insert(tk.END, quote, 'color')
scroll.pack(side=tk.RIGHT, fill=tk.Y)


probability of winning the lottery

This brings us to the end of this article where we have learned about Probability, Combinations and how these two can be used in Python to calculate the probability of winning a lottery.

If you wish to learn more about Python and the concepts of Machine Learning, upskill with Great Learning’s PG Program Artificial Intelligence and Machine Learning.



Please enter your comment!
Please enter your name here

16 + seven =