Many programming language functions provide a pseudo-random number generator which generates a number between 0.0 and 0.99999999…. And you might want to generate a random number with a different distribution from it.

In order to do this, get the inverse function of the cumulative distribution function of the distribution you wish to simulate, and then pass in the output of the pseudo-random number generator as input to this inverse function. The output of this inverse cdf function is the distribution you need.

Pseudocode:

inverseCDF(random(x))

Example:

Inverse CDF of 6-sided dice is "sort of"Â floor(6 * x + 1).

The + 1 is to account for the fact that random(x) can generate 0 but not 1.

So to simulate a dice, you need floor(6 * random(x) + 1).

For D&D replace 6 with 20 or number of sides your dice has.

Advertisements