r: Get full distribution of a roll

Description Usage Arguments Details Value Built-in Dice Arbitrary Precision See Also Examples

View source: R/roll.R

Description

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.

Usage

1
r(roll, precise = FALSE)

Arguments

roll

A roll expression (e.g., 2 * d6 + 5) or a data frame returned by r().

precise

Whether to return values with arbitrary precision.

Details

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.

Value

A data frame with four columns: outcome, n, d, and p.

Built-in Dice

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.

Arbitrary Precision

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.

See Also

roll, roll-plot, Dice.

Examples

1
2
# Get full distribution of 2d6 + 5
r(2 * d6 + 5)

droll documentation built on Aug. 7, 2021, 1:05 a.m.

Related to r in droll...