Learn R Programming

dice (version 1.2)

getEventProb: Calculate the probability of a specified set of dice-rolling events

Description

For a specified dice-rolling process, getEventProb calculates the probability of an event (i.e., a non-empty set of outcomes) that is specified by passing a list object in to eventList.

Usage

getEventProb(nrolls, ndicePerRoll, nsidesPerDie, eventList, orderMatters = FALSE)

Arguments

nrolls
A single positive integer representing the number of dice rolls to make
ndicePerRoll
A single positive integer representing the number of dice to use in each dice roll
nsidesPerDie
A single positive integer representing the number of sides on each die (getEventProb's dice-rolling process involves only one type of die per call)
eventList
A list object, each element of which is a vector that constrains a single dice roll in the dice-rolling process (see Details below)
orderMatters
A logical flag indicating whether the order of the elements of eventList should constrain the event space; if TRUE, eventList must specify constraints for every dice roll--i.e., it must contain exactly nrolls elements (some of which may be "empty" constraints listing all possible outcomes of a dice roll, i.e., a vector from ndicePerRoll to (ndicePerRoll * nsidesPerDie))

Value

A single number representing the probability of an event that meets the constraints of the specified dice-rolling process

Details

The crux of this function is eventList, which sets the conditions that acceptable dice-rolls must meet. E.g., to get the probability of rolling at least one 6 when rolling four six-sided dice, eventList would be list(6) and orderMatters would be FALSE; to get the probability of rolling a 6, followed by a 5, followed by either a 1, 2, or 3 when rolling three six-sided dice, eventList would be list(6,5,1:3) and orderMatters would be TRUE.

Examples

Run this code



## Probability of rolling at least one 6 when rolling four six-sided dice

getEventProb(nrolls = 4,
             ndicePerRoll = 1,
             nsidesPerDie = 6,
             eventList = list(6))

## Probability of rolling a 6, followed by a 5, followed by either a 1, 2,
## or 3 when rolling three six-sided dice

getEventProb(nrolls = 3,
             ndicePerRoll = 1,
             nsidesPerDie = 6,
             eventList = list(6, 5, 1:3),
             orderMatters = TRUE)

## Probability of rolling no 10's when rolling two ten-sided dice

getEventProb(nrolls = 2,
             ndicePerRoll = 1,
             nsidesPerDie = 10,
             eventList = list(1:9,1:9))


Run the code above in your browser using DataLab