Simulating rolls of Dice

lower

Image clicked and formatted by Mr. Chandra Sekhar for fermibot.wordpress.com


This post is entirely dedicated to the simulation of roll of fair dice. We will start with one dice and extend it to several of them. I will also provide a python code which you can use to do the simulations yourself.

Roll of One Dice

This is pretty simple. We will need a Uniform Random Number from 0 to 1 and then we need to use conditional statements used in conjunction with that do generate a dice. The algorithm is explained as follows.

Generate a Uniform random number u ~ U(0, 1)

  • If 0 ≤ u < 1⁄6,           return 1
    • else if 1⁄6 ≤ u < 2⁄6, return 2
      • else if 2⁄6 ≤ u < 3⁄6, return 3
        • else if 3⁄6 ≤ u < 4⁄6, return 4
          • else if4⁄6 ≤ u < 5⁄6, return 5
            • else if 5⁄6 ≤ u < 1, return 6

There you have it! This is your digital dice.

Because u can have an outcome between 0 and 1 at any given iteration, we would like to see how the outcomes look so in the long run. The histograms below represent that same.

dice_01_size700


Sum of Two Dice

This is the case where we observe sum of outcomes of two different dice. This will give out a different kind of distribution (a triangular distribution). We can see that below.

dice_02_size700


 Sum of Three Dice

dice_03_size700


Sum of Four Dice

dice_04_size700


Sum of Five Dice

dice_05_size700


Sum of Six Dice

dice_06_size700


Sum of Seven Dice

dice_07_size2000


Python Code and Output

Code used

The following snippet shows the code that has been used for the simulation

dice_python_source_code

I have also used python 3.5 to create a dice using the same logic. The screenshot is shown below. The input parameters are as follows

  • An initial seed value
  • the number of iterations

The output is a list of random integers in the range 1 to 6

dice_python_code

 


Codes for download:

  1. Python 3.5 codes can be found here
  2. Mathematica 11.0.0 codes can be found here

Please drop a comment below if you have downloaded any of these files. Thanks


End of the Post


.

.

.

.

.

.

.

.

.

.

.

.

.

 

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.