r.Rd
Return a data frame with most information necessary to work with the roll distribution: a column containing the possible outcomes of the roll expression, a column counting how many different ways each outcome can be obtained, a column with the associated densities, and a column with the associated probabilities. See below for more details.
r(roll, precise = FALSE)
roll | A roll expression (e.g., |
---|---|
precise | Whether to return values with arbitrary precision. |
A data frame with four columns: outcome
, n
, d
, and p
.
A roll expression is a piece of R code that describes a dice roll with or
without modifiers, e.g., 2 * d6 + 5
. This function processes roll
expressions in order to power both the roll and roll-plot family of
functions. Given an expression of this form it calculates the complete
distribution of the outcomes. This is possible because the distribution is
discrete and has a finite number of outcomes.
Standard dice notation should
mostly work out of the box, with the notable exception of NdX
, i.e., "roll
N
dice with X
faces and add the results". In this case, the user must
write N * dX
; this also means that, when translating "roll a die with X
faces and multiply the result by N
" to a roll expression, the user must
then write dX * N
. All other expressions involving dice can usually be
pasted straight into these functions.
For more details on what operations are supported, see the Dice S4 class.
It is possible to define any die with d()
, but some are already built-in.
Because of R's restrictions on what kind of object can be exported, they are
not readily available for the user, but can be used inside a roll expression
nonetheless These are the standard D&D dice: d4
, d6
, d8
, d10
, d12
,
d20
, and d100
.
Most dice programs that can calculate probabilities are forced to round their results due to the fact that these quantities might become exceptionally low when dealing with a lot of dice. This, however, can lead to error magnification.
In order to avoid rounding as much as possible, all functions described here
use Ryacas::yac_str()
to run computations symbolically. By default,
results are converted to numeric vectors just before returning to the user,
but one is able to access the symbolic strings returned by Ryacas by setting
precise = TRUE
.
# Get full distribution of 2d6 + 5 r(2 * d6 + 5)#> # A tibble: 11 × 4 #> outcome n d p #> <dbl> <dbl> <dbl> <dbl> #> 1 7 1 0.0278 0.0278 #> 2 8 2 0.0556 0.0833 #> 3 9 3 0.0833 0.167 #> 4 10 4 0.111 0.278 #> 5 11 5 0.139 0.417 #> 6 12 6 0.167 0.583 #> 7 13 5 0.139 0.722 #> 8 14 4 0.111 0.833 #> 9 15 3 0.0833 0.917 #> 10 16 2 0.0556 0.972 #> 11 17 1 0.0278 1