Introduction

When rolling fair 2d6, what value is most likely to be rolled? I answer this common question in two examples. Example 01 uses the dice package, while Example 02 uses a rudimentary user-defined function.

Example 01

To answer the problem posed, the dice package is used. The code is:

require(dice)
temp = data.frame()
for (i in 2:12){
    dp = getEventProb(nrolls = 1,
                      ndicePerRoll = 2,
                      nsidesPerDie = 6,
                      eventList = list(i))
    temp = rbind(temp,
                 cbind(Roll = i, Probability = dp))
}
pander(temp, caption = "", justify = "left")
Roll Probability
2 0.02778
3 0.05556
4 0.08333
5 0.1111
6 0.1389
7 0.1667
8 0.1389
9 0.1111
10 0.08333
11 0.05556
12 0.02778

The results show a table with the value of the roll, as well as the probability of that roll landing. Parameters for the 2d6 are entered as arguments to the dice::getEventProb() function.

Example 02

To answer the problem posed, a rudimentary user-defined function is used. The code is below:

dice.roll = function(sides, dice, value = F){
    # Create list to store combinations
    rolls = matrix(nrow = sides^(dice), 
                   ncol = dice+1)
    rolls[, 1] = c(rep(1:sides, times = sides^(dice-1)))
    rolls[, 2] = sort(rolls[, 1], decreasing = F)
    rolls[, 3] = rowSums(rolls[, 1:2])
    # Create table based on frequency
    freq = table(rolls[, 3])
    # Produce results based on value specified
    if (!value){
        temp = data.frame()
        for (i in ((dice):(dice*sides))){
            comb = as.vector(freq[names(freq) == i])
            prob = pbinom(q = 0,
                          size = 1,
                          prob = ((comb)/(sides^dice)),
                          lower.tail = F)
            temp = rbind(temp,
                         cbind(Roll = i, Probability = prob))
        }
        return(temp)
    } else {
        comb = as.vector(freq[names(freq) == value])
        prob = pbinom(q = 0,
                      size = 1,
                      prob = ((comb)/(sides^dice)),
                      lower.tail = F)
        return(prob)
    }
}

While not as comprehensive as the dice::getEventProb() function, the function does accept an argument named value, which when specified, will return the probability of rolling that value given the other parameters. To test the function:

# Roll 2d6, see probability of rolling all values
pander(dice.roll(6, 2), caption = "", justify = "left")
Roll Probability
2 0.02778
3 0.05556
4 0.08333
5 0.1111
6 0.1389
7 0.1667
8 0.1389
9 0.1111
10 0.08333
11 0.05556
12 0.02778
# Roll 2d6, see probability of rolling seven
dice.roll(6, 2, 7)
## [1] 0.1666667

While convenient, this function is rudimentary because (as written) it will not work when the number of dice specified is not equal to 2, e.g. dice.roll(6, 3) will fail. This is due to the current structure of the function, namely when specifying the number of columns to create in the initial matrix.

Session Info

For reproducibility, session information is included below.

sessionInfo()
## R version 3.3.1 (2016-06-21)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 14393)
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] pander_0.6.0 dice_1.2     gtools_3.5.0
## 
## loaded via a namespace (and not attached):
##  [1] magrittr_1.5    formatR_1.4     tools_3.3.1     htmltools_0.3.5
##  [5] yaml_2.1.13     Rcpp_0.12.7     stringi_1.1.1   rmarkdown_1.0  
##  [9] knitr_1.14      stringr_1.1.0   digest_0.6.10   evaluate_0.9