Activity: Intro to Python
Instructions:
- Work with a neighbor to answer the following questions
- You do not need to submit anything for this activity
Working in Python: To work in Python, you will connect to the DEAC OnDemand server:
https://sta279-f25.github.io/resources/rstudio_server/
The birthday problem
Suppose we have a class of 30 students. What is the probability that there is at least one shared birthday?
- Assume there are 365 days in a year
- Assume that each day is equally likely as a birthday
- Assume there are no multiple-birth siblings (e.g. twins, triplets, etc.) in the class
In a previous class activity, we wrote the following R code:
days <- 1:365 # days of the year
n_students <- 30
nsim <- 10000
results <- rep(NA, nsim) # store the simulation results
for(i in 1:nsim){
birthdays <- sample(days, n_students, replace=TRUE)
results[i] <- length(unique(birthdays)) < n_students
}
mean(results)In this class activity, we will re-write the code in Python.
Use the
np.arangefunction to create a 1-dimensional array,days, to store the days of the year (numbered 0 to 364)Use the
np.random.choicefunction to randomly choose birthdays for the class (shared birthdays are possible, so usereplace=Truein the function!)Check the number of distinct birthdays (hints: the
np.uniqueandlenfunctions may be helpful!). Are there 30 distinct birthdays?Use a
forloop to repeat the experimentnsim = 10000times, making sure to store the results. What is the probability of at least one shared birthday?