# Number of simulations
<- 10000
num_simulations
# Function to check if there are shared birthdays in a group
<- function(group_size) {
check_shared_birthday <- sample(1:365, group_size, replace = TRUE)
birthdays if (length(birthdays) == length(unique(birthdays))) {
return(FALSE) # No shared birthdays
else {
} return(TRUE) # Shared birthdays
}
}
# Simulate the Birthday Paradox
<- function(group_size) {
simulate_birthday_paradox <- 0
shared_birthday_count for (i in 1:num_simulations) {
if (check_shared_birthday(group_size)) {
<- shared_birthday_count + 1
shared_birthday_count
}
}return(shared_birthday_count / num_simulations)
}
# Test the Birthday Paradox simulation for different group sizes
<- 2:100
group_sizes <- numeric(length(group_sizes))
results
for (i in 1:length(group_sizes)) {
<- simulate_birthday_paradox(group_sizes[i])
results[i]
}
# Plot the results
plot(group_sizes, results, type = "l", xlab = "Group Size", ylab = "Probability of Shared Birthday", main = "Birthday Paradox Simulation")
abline(h = 0.5, col = "red", lty = 2) # Add a line at 0.5 for reference
The Birthday Paradox is a probabilistic problem concerning the likelihood that two people in a group share the same birthday. At first glance, it might seem like the probability is very low, but in reality, it’s higher than you might think.
The paradox is based on the fact that there are many possible combinations of people’s birthdays within a group. While it might appear that there is only a small chance that two people share a birthday, things change when we consider the entire group.
Let’s dive into action and use the R programming language to simulate the Birthday Paradox. We will see how the probabilities change as the group size increases.
The Birthday Paradox is a captivating example of how probability can be counterintuitive. Despite our intuitions, the probability of finding two people with the same birthday is higher than it seems. This paradox is an opportunity to explore the laws of probability and how they can surprise us.