## DEV Community is a community of 718,999 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Romain Guillemot

Posted on • Originally published at Medium on

# How to roll a dice in JavaScript?

## TL;DR

Use existing tools like — among others — boardgame.io or DiceApi ;)

## I want to understand how it works

Ok. Let’s start by defining the need: we want a function which returns a random number to simulate a dice. Something like this:

``````function rollDice() {
return /* some randomly generated number */
}
``````

As far as I know, every programming language has a built-in random function. In JavaScript, this would lead to Math.random:

``````function rollDice() {
return Math.random()
}
``````

That’s a good start: returning a random number. Be aware that Math.random is not “random enough” for serious things like cryptography, or casino games — read about Crypto.getRandomValues if that’s your business. Math.random is fair enough to roll a dice with friends. Let’s try it:

``````>> function rollDice() {
return Math.random()
}
>> rollDice()
<- 0.7367823644188911
``````

This `0.7367823644188911` is not really what we wanted… According to documentation, Math.random returns a decimal number between 0 (inclusive) and 1 (exclusive). For a 6-sided dice, we need an integer from 1 to 6. As a first guess, you may multiply by 6:

``````>> function rollDice() {
return Math.random()*6
}
>> rollDice()
<- 4.3380209914241235
``````

So we would have a random decimal number between 0 (inclusive) and 6 (exclusive). So far, so good. Next step would be to get integer values:

• If 0 ≤ x < 1, return 1
• If 1 ≤ x < 2, return 2
• If 2 ≤ x < 3, return 3
• If 3 ≤ x < 4, return 4
• If 4 ≤ x < 5, return 5
• If 5 ≤ x < 6, return 6

This can be done using Math.ceil — and a for-loop to console.log multiple rolls:

``````>> function rollDice() {
return Math.ceil(Math.random()*6)
}
>> for(let i = 0; i < 5; i++) console.log(rollDice())
1
4
5
3
6
``````

Or using Math.floor:

``````>> function rollDice() {
return Math.floor(Math.random()*6)
}
>> for(let i = 0; i < 5; i++) console.log(rollDice())
5
1
4
2
0 // WTF?
``````

Same, but different. This would change the returned value as follows:

• If 0 ≤ x < 1, return 0
• If 1 ≤ x < 2, return 1
• If 2 ≤ x < 3, return 2
• If 3 ≤ x < 4, return 3
• If 4 ≤ x < 5, return 4
• If 5 ≤ x < 6, return 5

To get the wanted result with Math.floor, you will have to add 1 before returning:

``````function rollDice() {
return 1 + Math.floor(Math.random()*6)
}
``````

So why using Math.floor instead of Math.ceil? Let’s see later. Now we have a function to simulate our 6-sided dice :)

Yes, but…

What if we want a 4-, 8-, 12- or 20-sided one? No big deal: you can change the magic number 6 in the code for a parameter, passing the maximum value for your dice. Something like this:

``````function rollDice(max) {
return 1 + Math.floor(Math.random()*max)
}

const rollDice4 = () => rollDice(4)
const rollDice6 = () => rollDice(6)
const rollDice8 = () => rollDice(8)
const rollDice12 = () => rollDice(12)
const rollDice20 = () => rollDice(20)
``````

Now we are here, why not removing the other magic number? This ugly 1 may become an other parameter:

``````function rollDice(min, max) {
return min + Math.floor(Math.random() * (max-min + 1))
}

const rollDice4 = () => rollDice(1, 4)
const rollDice6 = () => rollDice(1, 6)
const rollDice8 = () => rollDice(1, 8)
const rollDice12 = () => rollDice(1, 12)
const rollDice20 = () => rollDice(1, 20)
``````

Note that the Math.ceil version for this would be:

``````function rollDice(min, max) {
return (min-1) + Math.ceil(Math.random() * (max-min + 1))
}
``````

Math.floor looks cleaner to me now, but feel free to pick the one you prefer ;)

#### The ultimate dice

I was once inspired by a vision: “The Ultimate Display” by Ivan E. Sutherland, 1965. Among others, I like this quote:

There is no reason why the objects displayed by a computer have to follow the ordinary rules of physical reality with which we are familiar.

The final code with the min allows to simulate a dice which is not starting at 1. Moreover the max allows to simulate a uniform fair dice beyond “the ordinary rules of physical reality” — Imagine a 7-sided one. You can mimic your favorite dice game following its ordinary rules. It’s ok, really. But if you can imagine one, roll a dice which would never exist in reality ;)