Probability
Problem to Solve
If there are 23 people in your class, what are the chances that two of you have the same birthday? The answer is more than you might think! Rather than work out the math by hand, we can let the computer run an experiment for us, many times over, and count how often a match turns up.
In a file called probability.py in a folder called probability, implement a program that uses simulation to estimate how likely it is that, in a class of a given size, at least two students share a birthday.
Getting Started
Log into cs50.dev, click on your terminal window, and execute cd by itself. You should find that your terminal window’s prompt resembles the below:
$
Next execute
mkdir probability
to make a folder called probability in your codespace.
Then execute
cd probability
to change directories into that folder. You should now see your terminal prompt as probability/ $. You can now execute
code probability.py
to make a file called probability.py where you’ll write your code.
Background
In probability theory, the birthday problem (or birthday paradox) concerns the probability that, in a set of n randomly chosen people, some pair of them will have the same birthday.
By the pigeonhole principle, the probability reaches 100% when the number of people reaches 367, since there are only 366 possible birthdays (including February 29). Surprisingly, though, the probability reaches 99.9% with just 70 people, and 50% with just 23 people. These figures assume that each day of the year, excluding February 29, is equally likely to be someone’s birthday.
Those numbers can be computed exactly with a bit of math. But we can also estimate them with a simulation: generate a random set of 23 birthdays, check whether any two of them match, and then repeat that experiment many times. The fraction of experiments that produce a match is an estimate of the true probability, and the more experiments we run, the closer that estimate should get. Python’s random module makes it easy to generate the random birthdays.
Specification
- Your program should require two command-line arguments: first, the number of simulations to run, and second, the number of students in the class.
- If your program is executed with the incorrect number of command-line arguments, your program should print an error message of your choice (with
print). If the correct number of arguments are provided, you may assume that both are positive integers.
- If your program is executed with the incorrect number of command-line arguments, your program should print an error message of your choice (with
- In each simulation, your program should randomly generate one birthday per student, treating each of the 365 days of the year (excluding February 29) as equally likely, and then determine whether at least two students share a birthday.
-
After running all of the simulations, your program should print, on three separate lines, the number of simulations, the number of students, and the number of simulations in which there was at least one match, exactly as in the below.
After 900 simulations with 23 students there were 468 simulations with at least one match
Demo
Because the birthdays are random, your own counts will differ from run to run. But your program’s output should resemble the below.
$ python probability.py 900 23
After 900 simulations
with 23 students
there were 468 simulations with at least one match
$ python probability.py 1000 23
After 1000 simulations
with 23 students
there were 507 simulations with at least one match
$ python probability.py 950 25
After 950 simulations
with 25 students
there were 506 simulations with at least one match
Hints
- Recall that command-line arguments are available in
sys.argv, and that each of them is astr. You’ll need to convert them toints before you can use them as numbers. - You can represent a birthday as a number from 1 to 365. Take a look at
random.randint. - There’s more than one way to check a
listfor duplicates. Think about what happens to duplicate values when you convert alistinto aset. - Consider writing a helper function that runs a single simulation and returns
Trueif there’s a match andFalseotherwise. Then your main program need only call that function the right number of times and count theTrues.
How to Test
While check50 is available for this problem, you’re encouraged to first test your code on your own for each of the following. Because your program is random, its counts won’t exactly match the ones below, but they should be close, especially with many simulations.
- Run your program as
python probability.py. Your program should print an error message. - Run your program as
python probability.py 1000. Your program should print an error message. - Run your program as
python probability.py 10000 23. Your program should report roughly 5,000 simulations (about 50%) with at least one match. - Run your program as
python probability.py 10000 70. Your program should report nearly all 10,000 simulations (about 99.9%) with at least one match. - Run your program as
python probability.py 10000 10. Your program should report roughly 1,200 simulations (about 12%) with at least one match. - Run your program as
python probability.py 1000 1. Your program should report 0 simulations with at least one match, since one student can’t share a birthday with anyone! - Run your program as
python probability.py 1000 366. Your program should report all 1,000 simulations with at least one match, since 366 students can’t all have different birthdays when there are only 365 to go around.
Correctness
check50 cs50/problems/2026/fall/probability
Style
style50 probability.py
How to Submit
After you submit, wait a few minutes and then be sure to check your autograder results. If you see SUBMISSION ERROR: missing files (0.0/1.0), MissingFilesError, or the like, it means your file was not named exactly as prescribed (or you uploaded it to the wrong problem). Correctness in submissions entails everything from reading the specification, writing code that is compliant with it, and submitting files with the correct name. If you see this error, you should resubmit right away, making sure your submission is fully compliant with the specification. The staff will not adjust your filenames for you after the fact!
- Download your
probability.pyfile by control-clicking or right-clicking on the file in your codespace’s file browser and choosing Download. - Go to CS50’s Gradescope page.
- Click Problem Set 6: Probability.
- Drag and drop your
probability.pyfile to the area that says Drag & Drop. Be sure it has that exact filename! If you upload a file with a different name, the autograder likely will fail when trying to run it. Ensuring you have uploaded files with the correct filename is your responsibility! - Click Upload.
You should see a message that says “Problem Set 6: Birthday submitted successfully!”