Probability Puzzles in R
Peter Chi
Assistant Professor of Statistics
Chapter 1: The Classics
Chapter 2: Games with Dice
Chapter 3: Inspired by the Web
Chapter 4: Poker Games
factorial(n)
factorial(3)
$= 3! = 3 \times 2 \times 1$factorial(3)
6
choose(n,k)
choose(5,3)
$ = {5 \choose 3} = \frac{5!}{3! \times (5-3)!} $choose(5,3)
10
sample()
rbinom()
replicate()
for
, while
set.seed()
for(i in 1:10){
sum(sample(x = c(1,2,3,4,5,6), size = 2, replace = TRUE))
}
Storing the results:
rolls <- rep(NA, 10) for(i in 1:10){ rolls[i] <- sum(sample(x = c(1,2,3,4,5,6), size = 2, replace = TRUE)) }
rolls
3 6 3 9 9 3 6 11 9 10
my_function <- function(n){
answer <- n^3
return(answer)
}
my_function(10)
1000
Probability Puzzles in R