Sunday, August 4, 2024

Simulating a dice of any number of sides by rolling a different dice

Suppose for example that you have a dice of 6 sides and you want to obtain a random result between 1 and 8, how could you do it?

Well, one way is running my Perl script anydice.pl, rolling the dice the indicated number of times and then checking what is the result indicated by the tables printed by the program.

The method used by the program is explained in the paper "Simulating a dice with a dice" by B. Kloeckner.

$ perl anydice.pl
Use: anydice.pl [-d N] [-w] MAX
Print tables to get a random number from 1 to MAX by rolling a dice.
  -d N, -dN    Use a dice of N sides, 6 by default
  -w           Print the tables in HTML instead of ASCII
Important: When rolling a dice many times, if you get the result ?
then you must repeat ALL the rolls to get a correct probability.
$ perl anydice.pl -d6 8
A\B| 1 2 3 4 5 6
---+------------
 1 | 1 1 1 1 2 2
 2 | 2 2 3 3 3 3
 3 | 4 4 4 4 5 5
 4 | 5 5 6 6 6 6
 5 | 7 7 7 7 8 8
 6 | 8 8 ? ? ? ?

In this case, you must roll the D6 two times, A and B, and then look at the table to see what is the result. Note that if you get the result "?" when rolling the dice many times, then you must repeat all the rolls from the beginning, otherwise you will get certain results with a probability not equal to the others.