/DiceProbabilities

Probabilities of the sum results from rolling n dice with s sides

Primary LanguageMatlabApache License 2.0Apache-2.0

Dice Probabilities

Probabilities of the sum results from rolling n dice with s sides

Also available at: https://carlosvega.github.io/DiceProbabilities/

The problem statement reads:

Given a certain number d of dice and a number n of throws, write a function that plots the distribution of the sum of the dice (i.e. an histogram of how many times a particular sum occurs).

The solution developed is based on the two following articles, in which they use the solution shown in the book Introduction To Mathematical Probability (mirror) by James Victor Uspensky in pages 14 to 26 with the following formula depicted in page 24.

Formula

References

The code

The MATLAB code spans 3 files. The code is just quick scratch wibbly wobbly wimey stuff, so please, don't blame me. 😇

  • The function dice_prob_distr. This is the main function.
  • The function dice_prob, which calculates the probability for obtaining a given total p using n dice with s sides. The calculated probability follows the aforementioned formula, shaped as:

Formula 2nd version

A version in Python can be found in the following Jupyter Notebook from the interactive plot.

The MATLAB version prints a PDF such as this. Careful with big number of dice and the xticks.

Matlab result example