# 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:** 1\
**Showing post:** 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.

---

_[View the full topic](https://community.textexpander.com/t/roll-the-dice-with-textexpander/187)._
