In: Computer Science
Fill in the blanks in the following simulation for R, which finds the approximate variance of N, the number of rolls of a die needed to get the face having just one dot.
onesixth <- 1/6
sumn <- 0
sumn2 <- 0
for (i in 1:10000) {
n <- 0
while(TRUE) { ________________________________________
if (______________________________ < onesixth) break
}
sumn <- sumn + n
sumn2 <- sumn2 + n^2
}
approxvarn <- ____________________________________________
cat("the approx. value of Var(N) is ",approxvarn,"\n")
Solution:
onesixth <- 1/6
sumn <- 0
sumn2 <- 0
for (i in 1:10000) {
n <- 0
while(TRUE) { n <- n + 1
if (runif(1) < onesixth) break
}
sumn <- sumn + n
sumn2 <- sumn2 + n^2
}
approxvarn <- sumn2/10000 - (sumn/10000)^2
cat("the approx. value of Var(N) is ",approxvarn,"\n")
Sample Output:
the approx. value of Var(N) is 29.28388