I'm having a variable number of dice and dice types (eg. 4-side, 6-sided, etc), but dice types are never mixed. I need to calculate the probability of a certain outcome using any variation of dice and sides. For example, given 3 six-sided dice (which we'll write as 3d6), the probability of rolling 3 is 0.46% (100 / 6^3 = 0.46).
Probability = Number of desired outcomes ÷ Number of possible outcomes
- Number of possible outcomes: Number of sides to the power of the number of dice, eg. 6^3.
- Number of desired outcomes: ?
The application for this is that the dice formula can have modifier, ex. 3d6-5, and I'd like to calculate the chance that the outcome is zero or lower.
Thus, I was thinking that by adding up the percentages of to sum the total chance. Example (using anydice.com), for (3d6)-5, I'd add the chance for rolls of 3 (0.46%),4 (1.39%), 5 (2.78%), to come to the conclusion that there is a 4.63% chance of rolling equal to or lower than 0.
I'm looking for a somewhat efficient way to do this in Javascript somehow.
EDIT: Order matters! a roll of 6, 1 and 2 is not the same as 1, 2 and 6. This is not a combinatorics problem, as presented in the Medium article suggested by Barmar.
EDIT: Comments ask (rightfully so) for code. As I don't have a hypothesis on how to calculate it, I don't have much to that end, but this is what I do have:
function dieAvg(sides) {
return ((sides+1)/2);
}
// dice = number of dice
// die = sides, eg. 6 for a square dice
// mod = a modifier, positive or negative to apply to the result
function getDiceStats(dice, die, mod) {
let avg = (dieAvg(die)*dice) + mod;
let min = dice + mod;
let max = die*dice) + mod;
let zeroChance = 0; // TODO!
return [min,max,avg, zeroChance];
}
Somewhat related question (java): Calculate the number of ways to roll a certain number