Dice.Rd
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.
# 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)
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 | |
... | Numeric arguments. |
na.rm | A logical indicating whether missing values should be removed. |
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.
faces
A numeric vector with the die's faces.
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()
.
#> # A die with faces: #> [1] 1 2 3 4 5 6# Roll 1d6 1 * d6#> [1] 1#> [1] 8