Title: | A Collection of Functions to Experiment Dice Rolls |
---|---|
Description: | A collection of functions to simulate dice rolls and the like. In particular, experiments and exercises can be performed looking at combinations and permutations of values in dice rolls and coin flips, together with the corresponding frequencies of occurrences. When applying each function, the user has to input the number of times (rolls, flips) to toss the dice. Needless to say, the more the tosses, the more the frequencies approximate the actual probabilities. Moreover, the package provides functions to generate non-transitive sets of dice (like Efron's) and to check whether a given set of dice is non-transitive with given probability. |
Authors: | Gennaro Tedesco |
Maintainer: | Gennaro Tedesco: <[email protected]> |
License: | GPL-2 |
Version: | 1.0.0 |
Built: | 2025-01-29 04:53:56 UTC |
Source: | https://github.com/cran/Rdice |
Simulates a coin flip.
coin.flip(coins = 5, flips = 100, weights = c(0.5, 0.5), getExact)
coin.flip(coins = 5, flips = 100, weights = c(0.5, 0.5), getExact)
coins |
The number of coins to flip. If unspecified, it defaults to 5. |
flips |
The number of flips. If missing, it defaults to 100. |
weights |
A vector of probability weights to assign to each face of the coin; if unspecified, it defaults to a fair coin with equally likely faces. If specified, its lenght must obviously be a vector of length two whose values sum up to 1. |
getExact |
A vector containing values to be matched exactly, namely the function returns only those combinations containing all the above mentioned variables. Since this is a coin flip, values must be specified in the form |
The function is a particular case of dice.roll
, namely a roll with 2 faces and 1 die.
A table containing the frequencies for each of the two occurrences (head and tail) after the specified number of flips.
coin.flip(coins = 5, flips = 100)
coin.flip(coins = 5, flips = 100)
Calculates all possible combinations as result of rolling a set of dice. Similar permutations are identified under the same combination and counted as many times as many occurrencies. Thee user can choose wheter to match exact values or to perform partial matches.
dice.combinations(faces, dice, rolls, weights, getPartial, getExact, toSum = FALSE)
dice.combinations(faces, dice, rolls, weights, getPartial, getExact, toSum = FALSE)
faces |
The number of faces the dice have; if unspecified, it defaults to 6. |
dice |
The number of dice to roll; if unspecified, it defaults to 2. |
rolls |
The number of times to roll the die; if unspeciefid, it defaults to 5. |
weights |
A vector of probability weights to assign to each face of the die; if unspecified, it defaults to a fair die with weights |
getExact |
A vector containing values to be matched exactly, namely the function returns only those combinations containing all the above mentioned variables. |
getPartial |
A vector containing values to be matched partially, namely the function returns only those combinations containing at least one of the above mentioned variables. If missing, it defaults to c(1:faces), namemly the function returns all combinations. |
toSum |
A logical value, defaulting to FALSE. If TRUE, the function returns the sum of the frequencies of the matches (to be used together with getExact or getPartial) |
The function returns an object of class diceRoll, namely a list whose values are themselves data.table
objects, in turn, so that one can directly apply any data.table
function thereupon.
values |
If toSum = FALSE, a list of all possible combinations rolled, together with corresponding frequencies. If toSum = TRUE, the function returns the sum of all frequencies in correspondence of matched valules. |
The case face = 2 corresponds to the coin.flip
.
Makes use of dice.roll
.
dice.combinations(faces = 6, dice = 4, rolls = 100, getExact = c(3,5), getPartial = c(1,2), toSum = TRUE)
dice.combinations(faces = 6, dice = 4, rolls = 100, getExact = c(3,5), getPartial = c(1,2), toSum = TRUE)
Simulates rolling of a set of dice.
dice.roll(faces, dice, rolls, weights)
dice.roll(faces, dice, rolls, weights)
faces |
The number of faces the dice have; if unspecified, it defaults to 6. |
dice |
The number of dice to roll; if unspecified, it defaults to 2. |
rolls |
The number of times to roll the die; if unspeciefid, it defaults to 5. |
weights |
A vector of probability weights to assign to each face of the die; if unspecified, it defaults to a fair die with weights |
The function returns an object of class diceRoll, namely a list whose values are themselves data.table
objects, in turn, so that one can directly apply any data.table
function thereupon.
results |
The numerical results rolled. |
frequencies |
A table containing each single occurrency (permutation) of results and the corresponding frequencies. |
sums_freq |
A table containing the frequencies of the sums of the values obtained in each single roll by all the dice. A cumulative sum is provided too. |
exp_value_sum |
The expectation value of the sum of the values obtained. |
dice.roll(faces = 6, dice = 3, rolls = 5)
dice.roll(faces = 6, dice = 3, rolls = 5)
A dataset containing the four standard Efron's dice, non-transitive set of dice with winning probabilities of 2/3.
data(efron)
data(efron)
A data table with 4 columns. Each column represents a die with six faces.
data(efron) is.nonTransitive(efron, prob = 2/3)
data(efron) is.nonTransitive(efron, prob = 2/3)
Checks whether a given set of dice is non-transitive with given probability. If no probability is given, checks whether a given set of dice is generally non-transitive.
is.nonTransitive(df, prob)
is.nonTransitive(df, prob)
df |
A |
prob |
The non-transitive probability according to which to check for non-transitivity. If unspecified, the function checks for general non-transitivity. |
A logical value: TRUE or FALSE.
See also nonTransitive.generator
.
df <- data.frame( die1 = c(5,4,6,15), die2 = c(3,6,16,2), die3 = c(0,2,14,15), die4 = c(8,10,1,9) ) is.nonTransitive(df, prob = 9/16)
df <- data.frame( die1 = c(5,4,6,15), die2 = c(3,6,16,2), die3 = c(0,2,14,15), die4 = c(8,10,1,9) ) is.nonTransitive(df, prob = 9/16)
A dataset containing the Miwin set of dice. This set is non-transitively losing, with losing probabilities of 17/36.
data(miwin)
data(miwin)
A data table with 3 columns. Each column represents a die with six faces.
data(miwin) is.nonTransitive(miwin, prob = 17/36)
data(miwin) is.nonTransitive(miwin, prob = 17/36)
This function generates random non-transitive dice given the number faces and the corresponding non-transitive probabilities.
nonTransitive.generator(dice, faces, max_value = faces, prob, error = 0.001)
nonTransitive.generator(dice, faces, max_value = faces, prob, error = 0.001)
dice |
The number |
faces |
The number of faces of each die. |
max_value |
The maximum integer allowed as nominal value for the faces. Standard choices are usually |
prob |
The probability one wants the set of dice to be non-transitive. If unspecified, a set of dice with different non-transitive probabilities for each pairing will be generated. |
error |
Computational error to check for machine precision equality. It defaults to 0.001: no need to be specified. |
The function randomly generates sets of dice and stops as soon as a non-transitive set of dice matching the specified conditions is found and only if so. As such, it might happen that it never returns any value, should the condition for non-transitive dice not be matched. One may need to manually interrupt the run (time delay is provided, one could set it as max threshold.)
A data.table
containing the set of non-transitive dice matching the specified conditions.
See also is.nonTransitive
.
A dataset containing the Oskar van Deventer dice, non-transitive set of dice where A beats B,C,E; B beats C,D,F; C beats D,E,G; #' D beats A,E,F; E beats B,F,G; F beats A,C,G; G beats A,B,D. #' Consequently, for arbitrarily chosen two dice there is a third one that beats both of them.
data(oskar)
data(oskar)
A data table with 6 columns. Each column represents a die with six faces.
data(oskar) is.nonTransitive(oskar)
data(oskar) is.nonTransitive(oskar)