Skip to main content

JavaScript Numbers & Math

JavaScript Math.random

Written by Published

Math.random gives a random decimal between 0 and 1.

It includes 0 but never reaches 1.

Everything else is built by scaling that range.

Example

Example

javascript

const value = Math.random();

console.log(value >= 0 && value < 1);

The output is true.

The value itself is different every time, so the check is what matters.

Building a Range

Multiply to widen the range.

Math.floor turns it into a whole number.

Add the minimum to shift the range where you want it.

Syntax

Syntax

javascript

Math.floor(Math.random() * (max - min + 1)) + min;

This gives a whole number from min to max, both included.

A Number from 0 to 9

Multiply by 10 and floor the result.

Example

Example

javascript

const n = Math.floor(Math.random() * 10);

console.log(n >= 0 && n <= 9);
console.log(Number.isInteger(n));

Both are true on every run.

A Dice Roll

Add one to shift the range from 0-5 to 1-6.

Example

Example

javascript

function roll() {
  return Math.floor(Math.random() * 6) + 1;
}

const value = roll();

console.log(value >= 1 && value <= 6);

Every roll lands inside the range.

Any Range

The general formula covers both ends inclusively.

Example

Example

javascript

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

const value = between(10, 20);

console.log(value >= 10 && value <= 20);

Leaving out the + 1 would never produce the maximum.

Picking from an Array

Use the array length as the range.

Example

Example

javascript

const colours = ["red", "green", "blue"];
const pick = colours[Math.floor(Math.random() * colours.length)];

console.log(colours.includes(pick));

The picked value is always one of the three.

Not for Security

Math.random is predictable enough that it must never be used for passwords or tokens.

Use crypto.getRandomValues for anything security-related.

Example

Example

javascript

const value = Math.random();

console.log(typeof value === "number" && value < 1);

It is fine for games, samples and shuffling.

Complete Example

Complete Example

html

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript Math.random</title>
</head>
<body>

  <h1>Math.random</h1>

  <p id="out"></p>
  <button id="roll">Roll again</button>

  <script>
    function between(min, max) {
      return Math.floor(Math.random() * (max - min + 1)) + min;
    }

    function show() {
      const colours = ["red", "green", "blue"];
      document.getElementById("out").innerHTML =
        "Dice: " + between(1, 6) +
        "<br>Between 10 and 20: " + between(10, 20) +
        "<br>Colour: " + colours[Math.floor(Math.random() * colours.length)];
    }

    show();
    document.getElementById("roll").addEventListener("click", show);
  </script>

</body>
</html>

Try It Yourself

Run the above example in the Try It Editor.

Try rolling repeatedly:

Click the button several times and watch the values change.

Important Points

  • Math.random gives a decimal from 0 up to but not including 1.
  • Multiply to widen the range.
  • Math.floor makes it a whole number.
  • Add the minimum to shift the range.
  • Never use it for anything security-related.

Conclusion

Every random value you need is built from this one function.

The + 1 in the range formula is the part people get wrong.

For security, reach for the crypto API instead.