193k views
2 votes
Please code in python

Have you ever heard of the birthday paradox? If we take a normal year, it has 365 days. If we pick a person at random its birthday can be any of the 365 days, so the probability for each day is 1/365. How many person do we need to randomly sample in order to have two of them with the same birthday? If we take 366 people then we are 100% sure that two of them have the same birthday. But how many do we need to pick in order to have more than 50% chance to have to people with the same birthday? Around 180? Let us explore this is the following exercise.

Write a function randomNumbers with two parameters upTo and num. The function will generate num integers each between 1 and upTo. These integers should be added to a set (not a list!). At the end the function returns the number of unique numbers generated (which corresponds to the size of the set). If the function returns the same as num, it means that all generated numbers were different, but if the number returned is less than num than it means that a random number was selected more than once. If you try the function with upTo = 365, then this is the same as choosing birthdates randomly. What value should you give to num to have a high chance of getting the same birthday twice (the function returns less than num)? Try for example, 10, 25, 50, 100…

Sample run:

While drawing 10 random numbers between 1 and 365, we obtained 10 different numbers, and 0 collusions.

While drawing 25 random numbers between 1 and 365, we obtained 24 different numbers, and 1 collusions.

While drawing 50 random numbers between 1 and 365, we obtained 45 different numbers, and 5 collusions.

While drawing 100 random numbers between 1 and 365, we obtained 89 different numbers, and 11 collusions.

While drawing 180 random numbers between 1 and 365, we obtained 146 different numbers, and 34 collusions.

User Christina
by
8.1k points

1 Answer

6 votes

Final answer:

The 'birthday paradox' explores the probability of two people sharing the same birthday in a group. The Python function randomNumbers generates random numbers to simulate birthdays and identifies when duplicates occur. Simulations show that with just 23 people, there is over a 50% chance of a shared birthday.

Step-by-step explanation:

Exploring the Birthday Paradox with Python

To explore the birthday paradox, we will write a Python function called randomNumbers that takes two parameters: upTo (the range of days in a year to simulate birthdays) and num (the number of random 'people' or 'birthdays' to simulate). The function generates random numbers between 1 and upTo, and each number represents a person's birthday. The numbers are added to a set to ensure uniqueness (since sets cannot have duplicate elements) and finally, the function returns the number of unique birthdays generated.

When num exceeds 365, we're certain to have at least one duplicate birthday. But to find the smallest number of people needed in order to have a greater than 50% chance of a shared birthday, we will need to perform simulations with different values of num. Empirical tests suggest the number is surprisingly low, around 23 people.

Here is the Python function to run the simulation:

import random

def randomNumbers(upTo, num):
birthdays = set()
for _ in range(num):
birthdays.add(random.randint(1, upTo))
return len(birthdays)

Real-world simulations can then be run using various values for num. As the value of num approaches 23, we will begin to see that the number of unique birthdays is less than num, indicating a collision where at least two individuals share the same birthday.

User Dabo
by
8.4k points