Rolling the Dice 2 – Weighted Dice

This post builds on Rolling the Dice, using the code from there.

Sometimes, as a DM/GM, I want to be able to roll “biased” dice. Sometimes biased heavier, sometimes biased lighter.

How do we go about making a weighted die programmatically?

I had to do some research on that topic myself. The short answer is to use powers and roots. And roots are just inverse powers.

Let’s just say that we want a biased coin, a 2-sided die. Let’s just square the number of sides, so now we are rolling 1 to 4. What do we do when we get the results? We take the square root, but we need to round that. Up or down? Well, are we trying to bias to big numbers or small ones? floor the result to bias down, ceil to bias up.

First let’s add the options to the console command:

Alright, but now we need to add those functions to the Dice class:

I’ve highlighted the important lines here. To take the root, we do another power operation, but we invert the power. After that, we will either ceil or floor the result.

Okay, but we still need to call the functions.

Sweet, let’s test that out.

Using our 2-sided die, or rather 20 of them, we can see that the bias is working. How about testing the factor option we have set up. For that, we will need to roll something with more sides.

Hmmm. That didn’t seem to work. The low rolling is rolling high.

Let’s take a look at the numbers

When we take the square root of 1 to 4 we get 1, 1.41, 1.73, 2. When we floor these we do get 3x 1’s, and 1x 4’s.

But let’s take that up one more, to a 3-sided die. We now have 3x 1’s, 5x 2’s, and 1×3’s. The weights are all wrong. If we do a 4-sided die, the number of 3’s goes up to 7, and there’s 1 of the 4’s. Okay. This solution will not work for us.

We need to change the low function:

Let’s see how that works now!

Nice!

The repository for this post can be found on GitHub here .

The repository for this post can be found on GitLab here .

The repository for this post can be found on Lupe Code’s GitLab mirror here .