Probability Puzzles in R
Peter Chi
Assistant Professor of Statistics Villanova University
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) }
roll_dice(2)
7
replicate(10, roll_dice(2))
8 10 10 2 11 5 4 6 11 7
rolls <- replicate(10, roll_dice(2))
rolls
8 10 10 2 11 5 4 6 11 7
table(rolls)
rolls
2 4 5 6 7 8 10 11
1 1 1 1 1 1 2 2
rolls <- replicate(100, roll_dice(1))
sum(rolls == 3)
22
if(sum(rolls == 3) > 17) print("The value of 3 was rolled more than 17 times")
"The value of 3 was rolled more than 17 times"
if(sum(rolls == 3) > 17 | sum(rolls == 4) > 17) print("The value of 3 or 4 was rolled more than 17 times")
"The value of 3 or 4 was rolled more than 17 times"
Probability Puzzles in R