Roll the dice with TextExpander!

Here’s a fun Snippet to try, especially if you like board games, and it’s a great way to get your feet wet with scripting. Create a Snippet with JavaScript as the Content Type, and put this code into the content field:

// Function to generate a random number between 1 and 20
function getRandomNumber(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}


// Generate the random number and output it
getRandomNumber(1, 20);

This simulates rolling a 20-sided die by generation a random number from 1-20. I set my abbreviation for this Snippet to ;d20.

You can change the 20 to another number to emulate another die. For instance, change 20 to 6 and it behaves like a traditional six-sided die.

This is so cool and fun, @josh.centers! Rolling for +20 on productivity today lol :crossed_fingers:

2 Likes

Shouldn’t the abbreviation be something about roll for initiative? ;rfi

1 Like

You could totally create different Snippets for different kinds of rolls. For instance, if you have a +2 bonus to initiative, you could bake in that math in a separate Snippet:

// Function to generate a random number between 1 and 20, with a +2 modifier
function getRandomNumber(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min + 2;
}

// Generate the random number and output it
getRandomNumber(1, 20);

You can play with these if you’re subscribed to the Community Snippets Public Group by expanding com.d20 and com.rfi.