0

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.

Anydice.com

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

svenema
  • 1,766
  • 2
  • 23
  • 45
  • Do all dice in a given throw have the same number of sides? are the fair dice? – Tomer Amir Jul 23 '20 at 13:10
  • https://stackoverflow.com/questions/41586026/counting-number-of-possible-combination-to-reach-a-sum-with-a-die https://medium.com/amiralles/combinatorics-counting-dice-combinations-using-javascript-52fdc4a1429c – Barmar Jul 23 '20 at 13:10
  • Please show what attempts you've made to solve the problem yourself. – Heretic Monkey Jul 23 '20 at 13:11
  • @TomerAmir: All dice have same number of sides, and are fair (not weighted?) dice. – svenema Jul 23 '20 at 13:16
  • @Barmar: The Medium article covers combinatorics instead of permutations. Also, it's not very clear on how to call the function, I'm getting infinite loops when using the presented function, probably not using the (not very clear named) parameters. It may be close to a solution, but I fail to decypher it. – svenema Jul 23 '20 at 13:41
  • Post what you're trying and we can help you fix it. – Barmar Jul 23 '20 at 14:02
  • Code added as per request. I'm sorry that it's not much; my mind hasn't come up yet with any solution that doesn't involve lots of loops.. – svenema Jul 23 '20 at 14:10

0 Answers0