1. Simulate $n$ rolls of a fair dice. Output the relative frequency of "6". dice <- function(n) { freq.prob.list <- NULL six.num <- 0 for(i in 1:n) { if(runif(1) < 1/6) six.num <- six.num + 1 freq.prob.list <- c(freq.prob.list, six.num/i) } return(freq.prob.list) } Example: > dice(1000)[1000] [1] 0.157 2. Robin and Sheriff aim at the target and hit it with probs 0.8 and 0.7, respectively. If they shut once, independently of each other, what is the probability that the target is hit. > 0.8 + 0.7 - 0.8*0.7 [1] 0.94 archers2 <- function(p, n) { hit <- 0 prop <- NULL for(i in 1:n) { Robin <- 0 Sheriff <- 0 if(runif(1) < p[1]) Robin <- 1 if(runif(1) < p[2]) Sheriff <- 1 if(Robin + Sheriff > 0) hit <- hit + 1 prop <- c(prop, hit/i) } return(prop) } > archers2(c(0.8, 0.7), 1000)[1000] [1] 0.939 3. Out of 100 coins one has heads on both sides. One coin is selected at random and flipped two times. What is the probability og 2 heads. Sol: 1/4 99/100 + 1 * 1/100 = 103/400 = 0.2575. twoheaded <- function(n) { proportion <- NULL twoheads <- 0 for(i in 1:n) { coin <- "GOOD" if(runif(1) < 0.01) coin <- "BAD" if(coin == "GOOD" && runif(1) < 0.25) twoheads <- twoheads + 1 proportion <- c(proportion, twoheads/i) } return(proportion) } > twoheaded(1000)[1000] [1] 0.25 4. In the group of N people, what is the prob that there is a at least one match in their birthdays. [no leap years involved] Exact: exabirth_function(n) { prob_1 for ( i in 1: n) prob_prob * (365-i+1)/365 1-prob } > exabirth(10) [1] 0.1169482 > exabirth(23) [1] 0.5072972 > exabirth(100) [1] 0.9999997 Simulation: matchbirth <- function(k, people=23) { mat_0 for(j in 1:k){ a_sample(seq(1,365), people, replace=T) aa_sort(a) for(i in 1:(length(aa)-1)) {if (aa[i]==aa[i+1]) {mat_mat+1 break} } } cat( mat/k, "\n") } > matchbirth(100, 23) 0.46