Roll ‘dem Bones!

You would think that I’d be deeply into Dungeons and Dragons, but no. I can’t stand the game. I find it tedious and predictable, boring. But I did enjoy rolling all those dice. Who knew that a 20-side die is a thing — and that rolling a “nat 20” is a big deal?

One of the ways I passed time while everyone else enjoyed the game was to repeatedly roll my dice. In the typical D&D set, you have seven die:

  • One four-sided die.
  • One six-sided die, which is the typical die normal humans are familiar with.
  • One eight-sided die.
  • One twelve-sided die.
  • One twenty-sided die.
  • Two ten-sided die, which are rolled together to obtain values from 0 through 99.

You can find a 100-sided die in the gaming stores, but most sets are sold with two ten-sided die, one with numbers 0 through 9 and the other with values 00 through 90. As in C and other programming languages, I admire that the 100-sided die values are from zero to 99. The other dies all start with the number 1.

Anyhoo . . .

As I sat waiting my turn, one of the games I’d play was to repeatedly roll all my dice. When a die revealed it’s maximum value, I’d set it aside and continue to role until another die reached its maximum value. As I rolled, I counted the number of times it took before finally every die had rolled its top value. Such fun.

The following code represents the first step in creating my “Dear Lord This Is So Boring” dice-rolling game I played while others around me enjoyed D&D. It uses a roll() function that I’ve written about before, but this time I’ve modified the function to handle dice with more than six sides.

2025_07_26-Lesson.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* rolling an 's' sided die */
int roll(int s)
{
    return( rand() % s + 1 );
}

int main()
{
    const int die = 6;
    /* d4, d6, d8, d12, d20, d100 */
    const int sides[] = {
        4, 6, 8, 12, 20, 100
    };
    int d[die];        /* six different die */
    int x;

    /* seed the randomizer */
    srand( (unsigned)time(NULL) );

    /* throw dem bones! */
    for( x=0; x<die; x++ )
        d[x] = roll( sides[x] );

    /* show results */
    printf("%5s%5s%5s%5s%5s%5s\n",
            "D4","D6","D8","D12","D20","D100"
          );
    for( x=0; x<die; x++ )
        printf("%5d",d[x]);
    putchar('\n');

    return 0;
}

The roll() function is passed the number of sides, which helps generate the proper return value based on the die chosen. For example, the function returns 1 through 20 for rolling a 20-sided die. The D100 dice returns 1 through 100, not zero through 99, which is how the game is played.

In the main() function, constant array sides[] holds the number of sides for each die. This value is used in the for loop to help roll each die. The resulting roll values are saved in array d[] and then output in a second for loop.

Here’s a sample run:

   D4   D6   D8  D12  D20 D100
    3    6    2   10   14   17

In the game I played, the above output means that the six-sided die is eliminated; 6 is its maximum value. I’d continue rolling the rest of the dice until each maxes out. But this code proves only that the process works and rolls the dice. For next week’s Lesson, I complete the program, rolling all the dice until each maxes out.

Leave a Reply