Probability Puzzles in R
Peter Chi
Assistant Professor of Statistics Villanova University
Setup
Assumptions
Simulating the probability of rolling a 12
counter <- 0
roll <- roll_dice(2) roll
12
if(roll == 12){ counter <- counter + 1 }
counter
1
roll_dice <- function(k){
all_rolls <- sample(c(1,2,3,4,5,6),
k,
replace = TRUE)
final_answer <- sum(all_rolls)
return(final_answer)
}
Simulating the probability of rolling a 12
counter <- 0 for(i in 1:10000){ roll <- roll_dice(2) if(roll == 12){ counter <- counter + 1 } } p_twelve <- counter / 10000
print(p_twelve)
0.0278
pbirthday(10)
0.1169482
room_sizes <- c(1:10)
match_probs <- sapply(room_sizes, pbirthday)
print(match_probs)
0.000000000 0.002739726 0.008204166 0.016355912 0.027135574 0.040462484 0.056235703 0.074335292
0.094623834 0.116948178
plot(match_probs ~ room_size)
Probability Puzzles in R