Benezer
Benezer

Reputation: 1

what is the equivalent R code for this experiment?

Estimate the probability that the sum of five dice is between 15 and 20, inclusive.

my attempt:


myex<-replicate(100,{
   #creat a vector of 5 die
   mydie <- sample(1:6, 5, replace= T)
   #sum the value of all 6 die
   sum = mydie[1]+mydie[2]+mydie[3]+mydie[4]+mydie[5]
})
#mean with the conditions
mean(myex >=15 & myex<=20)

am i wrong?

Upvotes: 0

Views: 59

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173803

You can get the exact probability by enumerating all possibilities

all_possible <- rowSums(do.call(expand.grid, lapply(1:5, \(x) 1:6)))

Which looks like this:

hist(all_possible)

Now we just count the number of these values that are between 15 and 20.

sum(all_possible >= 15 & all_possible <= 20)/length(all_possible)
#> [1] 0.5570988

To get an estimate of this value, we can get a sample of 5 independent dice rolls 1000 times like this instead, then use the same code to get the probability of finding the sum between 15 and 20:

samp <- replicate(1000, sum(sample(6, 5, TRUE)))

hist(samp)

sum(samp >= 15 & samp <= 20)/length(samp)
#> [1] 0.554

Created on 2023-01-15 with reprex v2.0.2

Upvotes: 2

Related Questions