# Roll the dice with TextExpander!

**URL:** <https://community.textexpander.com/t/roll-the-dice-with-textexpander/187>\
**Category:** TextExpander Talk\
**Tags:** snippets\
**Created:** [April 30, 2025, 4:48pm UTC](https://community.textexpander.com/t/roll-the-dice-with-textexpander/187 "2025-04-30T16:48:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh.centers](https://sea3.discourse-cdn.com/textexpander/user_avatar/community.textexpander.com/josh.centers/32/8_2.png) [@josh.centers](https://community.textexpander.com/u/josh.centers)\
**Post date:** [April 30, 2025, 4:48pm UTC](https://community.textexpander.com/t/roll-the-dice-with-textexpander/187/1 "2025-04-30T16:48:01Z")

</div>

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:

```auto
// 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.

---

<div class="post-metadata">

**Author:** ![MaguriteWeeks](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@MaguriteWeeks](https://community.textexpander.com/u/MaguriteWeeks)\
**Post date:** [April 30, 2025, 5:49pm UTC](https://community.textexpander.com/t/roll-the-dice-with-textexpander/187/2 "2025-04-30T17:49:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ScottJ](https://sea3.discourse-cdn.com/textexpander/user_avatar/community.textexpander.com/scottj/32/132_2.png) [@ScottJ](https://community.textexpander.com/u/ScottJ)\
**Post date:** [April 30, 2025, 8:16pm UTC](https://community.textexpander.com/t/roll-the-dice-with-textexpander/187/3 "2025-04-30T20:16:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![josh.centers](https://sea3.discourse-cdn.com/textexpander/user_avatar/community.textexpander.com/josh.centers/32/8_2.png) [@josh.centers](https://community.textexpander.com/u/josh.centers)\
**Post date:** [April 30, 2025, 8:51pm UTC](https://community.textexpander.com/t/roll-the-dice-with-textexpander/187/4 "2025-04-30T20:51:27Z")

</div>

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:

```auto
// 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](https://snippets.textexpander.com/group/fa796ecbe167aeb836aced0a65e43b94) by expanding `com.d20` and `com.rfi`.
