Learn R Programming

droll (version 0.1.0)

Dice: An S4 class to represent dice

Description

A virtual representation of a die that supports the same arithmetic operations as a numeric scalar, with the special property that, when operated on, its value is randomly sampled from the die's faces. See below for more details.

Usage

# S4 method for Dice
show(object)

# S4 method for Dice,numeric Ops(e1, e2)

# S4 method for numeric,Dice Ops(e1, e2)

# S4 method for Dice,Dice Ops(e1, e2)

# S4 method for numeric,Dice *(e1, e2)

# S4 method for Dice Math(x)

# S4 method for Dice Math2(x, digits)

# S4 method for Dice Summary(x, ..., na.rm = FALSE)

Arguments

object

A Dice object.

e1

A numeric scalar or a Dice object.

e2

A numeric scalar or a Dice object.

x

A Dice object

digits

Number of digits to be used in round() or signif().

...

Numeric arguments.

na.rm

A logical indicating whether missing values should be removed.

Slots

faces

A numeric vector with the die's faces.

Operations

By default, when printed, an object of this class returns a numeric vector with all of its faces. In order to actually "roll" the die (that is, get one of its faces at random), one can simply operate on it. Any arithmetic expression should trigger a die into sampling its faces, even dX + 0 and 1 * dX.

All standard arithmetic operations are supported, along with comparisons, logic assertions, mathematical functions, and summaries: every group described in S4groupGeneric except for Complex. Note that, when used in other situations, like c(), the die will return all of its faces.

These functions also work in the exact same way as they would with regular numeric scalars, with the exception of multiplication. With the goal of supporting the very common operation NdX ("rolling N dice with X faces and adding the results"), the multiplication symbol behaves differently depending on the context: N * dX will work as NdX and dX * N will work as N x dX ("rolling a die with X faces and multiplying the result by N").

The roll and roll-plot families of functions make ample use of roll expressions like the ones described here. They even support some built-in dice that can be used without being created with d().

Details

This S4 class extends numeric with the goal of creating an interactive die inside of R. In short, an instance of this class functions as a numeric scalar for most intents and purposes except that, when its value is needed, it returns one of its faces at random.

For more information on exactly what operations are supported, see the Operations section below. To learn more about how to create an object of this class, see the dice creating function d(). For roll distributions, see the roll family. For plotting those distributions, see the roll-plot family.

See Also

d(), roll, roll-plot.

Examples

Run this code
# NOT RUN {
set.seed(42)

# Create some dice with d()
d6 <- d(6)
d20 <- d(20)

# Print faces from die
d6

# Roll 1d6
1 * d6

# Check if an attack hits and deal damage
if (d20 + 8 >= 12) {
  print(4 * d6)
} else {
  print(0)
}
# }

Run the code above in your browser using DataLab