Home > Back-end >  Finding probability that the sum of numbers to be exactly 70 in R?
Finding probability that the sum of numbers to be exactly 70 in R?

Time:10-13

The question is to find the probability that the sum of numbers is equal to exactly 70 from 20 dice thrown.

I have created a matrix

A = matrix(NA, nrow=20, ncol=1e4)
for (i in 1:20) {
  A[i,]=sample(1:6,1e4,replace=TRUE)
}

I am stuck on what to do next. I should be creating a vector s of length 10^4 such that s[i] = sum of the column in position i.

CodePudding user response:

You can create a 20 x 10,000 matrix of sample dice rolls, then calculate the column sums, then count those that are exactly 70. The probability estimate is just this result divided by 10,000. So this one-liner should suffice:

length(which(colSums(replicate(1e4, sample(1:6, 20, replace = TRUE))) == 70))/1e4
#> [1] 0.0526
  • Related