# Dice: An S4 class to represent dice In droll: Analyze Roll Distributions

## 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

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23``` ```## S4 method for signature 'Dice' show(object) ## S4 method for signature 'Dice,numeric' Ops(e1, e2) ## S4 method for signature 'numeric,Dice' Ops(e1, e2) ## S4 method for signature 'Dice,Dice' Ops(e1, e2) ## S4 method for signature 'numeric,Dice' e1 * e2 ## S4 method for signature 'Dice' Math(x) ## S4 method for signature 'Dice' Math2(x, digits) ## S4 method for signature '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.

## 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.

## 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()`.

`d()`, roll, roll-plot.
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18``` ```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) } ```