Skip to content

Instantly share code, notes, and snippets.

@kwichmann
Created September 16, 2016 08:55
Show Gist options
  • Select an option

  • Save kwichmann/369455611cce8e4f6e482daafb0dee68 to your computer and use it in GitHub Desktop.

Select an option

Save kwichmann/369455611cce8e4f6e482daafb0dee68 to your computer and use it in GitHub Desktop.
Simulates rolling two dice pools against each other. Most sixes win, if tied, most fives win, if tied most fours win ... and so on.
roll <- function(n)
sample(1:6, n, replace = T)
number_of <- function(roll, n)
sum(roll == n)
win <- function(roll1, roll2) {
for (n in 6:1) {
if (number_of(roll1, n) > number_of(roll2, n)) return(1)
if (number_of(roll1, n) < number_of(roll2, n)) return(2)
}
0
}
testruns <- 100000
winmatrix <- matrix(0, nrow = 8, ncol = 8)
tievector <- 1:8
for (i in 1:8)
for (j in 1:8) {
results = NULL
for (k in 1:testruns) {
roll1 <- roll(i)
roll2 <- roll(j)
results <- append(results, win(roll1, roll2))
}
winmatrix[i, j] = number_of(results, 1) / testruns
if (i == j)
tievector[i] <- number_of(results, 0) / testruns
}
winmatrix
tievector
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment