Simulating D&D dice, 6 sided dice, coin-toss or any distribution with pseudo- random number generators.

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s