Birthday Paradox coded in Python. This is for mid-level programmers who have the basic knowledge of the element used in this Python program. We have run here Monte Carlo simulation.

## Theory : Program of Birthday Paradox in Python

This is not a Paradox it is only a surprising result of probability. We have here chosen random numbers as birthday and we are founding their is very much high probability of two person having a birthday on same date. We have used here Monte Carlo simulation of 100000 times and find out whether two person have the same birthday in simulation of n persons and we are seeing a very high probability of it happening.

### Source code : Program of Birthday Paradox in Python

import datetime, random def inputBirthdays(noOfBirthdays): birthdays = [] for i in range(noOfBirthdays): # The year is unimportant for us birthday date only important and the birthdays keep on repeating every year. startOfYear = datetime.date(2001, 1, 1) # inputing random day from random function in any year randomNoOfDays = datetime.timedelta(random.randint(0, 364)) birthday = startOfYear + randomNoOfDays birthdays.append(birthday) return birthdays def Compare(birthdays): """This function will return the birthday which has appeared more than once """ if len(birthdays) == len(set(birthdays)): return None #Birthday are uique so None is returned. # Each birthday is compared to anoter birthday for a, birthdayA in enumerate(birthdays): for b, birthdayB in enumerate(birthdays[a + 1 :]): if birthdayA == birthdayB: return birthdayA # Matched birthday is returned. # Display the intro: print('''Birthday Paradox, by Archit architgulia@gamil.com The Birthday Paradox is that n random people have very high probalbility of occurence of same birthday date . In this program monte carlo simpulation is used to check whether the random probability is so high. This is not a paradox but a surprising result ''') # Setting up a tuple of monthly date Months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun','Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec') while True: # keep on asking till the user gives correct input print('How many birthdays shall I generate? (Max 100)') response = input('> ') if response.isdecimal() and (0 < int(response) <= 100): numBDays = int(response) break # User has entered a valid amount. print() # Generating and displaying Birthaday print('Here are', numBDays, 'birthdays:') birthdays = inputBirthdays(numBDays) for i, birthday in enumerate(birthdays): if i != 0: # Displaying a comma after first birthday and every birthday print(', ', end='') monthName = Months[birthday.month - 1] dateText = '{} {}'.format(monthName, birthday.day) print(dateText, end='') print() print() # To determine if two birthdays match match = Compare(birthdays) # Displaying the result print('In Simulation, ', end='') if match != None: monthName = Months[match.month - 1] dateText = '{} {}'.format(monthName, match.day) print('multiple people having a birthday on', dateText) else: print('there is no matched birthdays.') print() # Running 100,000 simulations: print('Processing', numBDays, 'random birthdays 100,000 times...') input('Press Enter to start...') print('Let run another 100,000 simulations.') birthmatch = 0 # How many simulations has matching birthdays in them. for i in range(100_000): # Reporting on the progress after every 10,000 simulation if i % 10_000 == 0: print(i, 'simulations run...') birthdays = inputBirthdays(numBDays) if Compare(birthdays) != None: birthmatch = birthmatch + 1 print('Processed 100,000 simulations run.') # Displaying simulation results: probability = round(birthmatch / 100_000 * 100, 2) print('Out of processed of 100,000 simulations of', numBDays, 'people, there are a') print('matched birthdays in the group', birthmatch, 'in times. That means') print('that is ', numBDays, 'people have a', probability , '% chance of to') print('having a matched birthday in group.') print('That is much number of time than thought!')

## Output :

RESTART: C:/Users/ARCHIT/Desktop/new python progrm/editedbirthday paradox.py

Birthday Paradox, by Archit architgulia@gamil.com

The Birthday Paradox is that n random people have very high probalbility

of occurence of same birthday date .

In this program monte carlo simpulation is used to check whether the random

probability is so high.

This is not a paradox but a surprising result

How many birthdays shall I generate? (Max 100)

> 27

Here are 27 birthdays:

Jun 15, Jul 16, Feb 17, Oct 12, Oct 1, Sep 6, Apr 16, Aug 24, Mar 28, Dec 2, Oct 6, Dec 18, Jan 18, Oct 31, May 2, Aug 1, Oct 12, Oct 4, Dec 28, Dec 20, Sep 15, Mar 13, Apr 9, Nov 20, Sep 14, Mar 17, Dec 9

In Simulation, multiple people having a birthday on Oct 12

Processing 27 random birthdays 100,000 times…

Press Enter to start…

Let run another 100,000 simulations.

0 simulations run…

10000 simulations run…

20000 simulations run…

30000 simulations run…

40000 simulations run…

50000 simulations run…

60000 simulations run…

70000 simulations run…

80000 simulations run…

90000 simulations run…

Processed 100,000 simulations run.

Out of processed of 100,000 simulations of 27 people, there are a

matched birthdays in the group 62795 in times. That means

that is 27 people have a 62.8 % chance of to

having a matched birthday in group.

That is much number of time than thought!

>>>

## Conclusion:

The program is running fine.

Program of Birthday Paradox coded in Python. This is for mid-level programmers who have the basic knowledge of the element used in this program. For more programs click below:

Python Programs Archives – CS Study