Learn R Programming

dice (version 1.2)

getSumProbs: Calculate the probabilities of all possible outcome sums of a dice roll

Description

For a specified number of dice with a specified number of sides per die (and dropping a specified number of dice--those with either the lowest or highest values), getSumProbs calculates the probabilities of all possible outcome sums (i.e., all possible sums of those dice whose results are not dropped); the function also accommodates modifiers (either to each die roll or to the sum), such as rolling five four-sided dice and adding 1 to the outcome of each roll, or rolling one twenty-sided die and adding 12 to the outcome. (Such modified rolls frequently occur in the context of role-playing games, e.g., Dungeons & Dragons, Mutants & Masterminds, or BESM.)

Usage

getSumProbs(ndicePerRoll, nsidesPerDie, nkept = ndicePerRoll, dropLowest = TRUE, sumModifier = 0, perDieModifier = 0, perDieMinOfOne = TRUE)

Arguments

ndicePerRoll
A single positive integer representing the number of dice to roll
nsidesPerDie
A single positive integer representing the number of sides on each die (getSumProbs's dice-rolling process involves only one type of die per call)
nkept
A single positive integer representing the number of dice whose values to include when calculating the sum (the dice to be kept will always be those with the highest values)
dropLowest
A single logical indicating whether to drop the lowest outcome values (FALSE drops the highest values instead)
sumModifier
A single integer representing an amount to add to or subtract from the outcome sum
perDieModifier
A single integer representing an amount to add to or subtract from each die roll
perDieMinOfOne
A logical flag indicating whether each die roll should be considered to have a minimum value of 1 (as is often true in role-playing-game contexts)

Value

probabilities
A matrix with a row for each possible outcome sum and three columns: one that lists each sum, one for the probability of that sum, and one for the number of ways to roll that sum
average
A single number representing the expected value of the specified dice-rolling process

References

This function's implementation originated with the ideas presented in the following forum thread:

http://www.enworld.org/showthread.php?t=56352&page=1&pp=40

Examples

Run this code



## Rolling four six-sided dice and keeping the three highest die rolls

getSumProbs(ndicePerRoll = 4,
            nsidesPerDie = 6,
            nkept = 3)

## Rolling five four-sided dice and adding 1 to each die roll

getSumProbs(ndicePerRoll = 5,
            nsidesPerDie = 4,
            perDieModifier = 1)

## Rolling one twenty-sided die and adding 12 to the result

getSumProbs(ndicePerRoll = 1,
            nsidesPerDie = 20,
            sumModifier = 12)

Run the code above in your browser using DataLab