Is what I did correct in making a code for this problem?
My goal is to find the mean and variance of the total score of the team when for any pair of gamers who throw the same number, the team scores 1 point.
die <- function(n){sample(1:6, n, replace = TRUE)}
A <- function(Die){
int <- table(Die)
sum(as.integer(int/2))
}
rolls <- 10000
players <- 10
scoreA <- sapply(1:rolls, function(x) {
rolled <- die(players)
A(rolled)
})
mean(scoreA)
var(scoreA)
A quick mental run through the problem should tell us approximately the answer we should expect.
If there are 10 players, then we have 5 pairs of players. Think about any one of those pairs. Since the throws are all independent, it doesn't matter what the first player throws; there is a one-in-six chance that the second player will throw the same number. Since the pair gets one point if the numbers match, the expected value in terms of points for a single pair throwing the dice will be 1 point * 1/6 = 1/6 points. Since there are five pairs on the team, the expected value of the team's score for each round will be 5 * 1/6 points = 5/6, or about 0.8333.
When I run your code, I get a value for
mean(scoreA)
of 3.5, so clearly this isn't correct.A naive implementation that simulates each throw would be:
And we can use the
replicate
function to run the simulation as many times as we like to getscoreA
:And the results are: