Implementation guide

How to generate random numbers in JavaScript

This page focuses on the implementation side of random number generation in JavaScript: basic floats, integer ranges, unique values, and the point where you should switch from Math.random() to crypto APIs.

1. Start with Math.random()

Math.random() returns a floating point number greater than or equal to 0 and less than 1. It is the base primitive for most non-security JavaScript random number tasks.

const value = Math.random();
console.log(value); // 0.123456789...

2. Generate integers in a range

To generate an integer between min and max inclusive, multiply by the size of the range, floor the result, and add the minimum value.

function getRandomInt(min, max) {
  const minCeil = Math.ceil(min);
  const maxFloor = Math.floor(max);
  return Math.floor(Math.random() * (maxFloor - minCeil + 1)) + minCeil;
}

console.log(getRandomInt(1, 10));

3. Generate floating point values

For decimals, keep the value as a float and scale it to the desired range. Round only when the presentation layer needs fewer digits.

function getRandomFloat(min, max, decimals = 2) {
  const random = Math.random() * (max - min) + min;
  return Number(random.toFixed(decimals));
}

console.log(getRandomFloat(5, 12, 3));

4. Generate a unique set

When you need several unique values in a small range, track what has already been used with a Set.

function getUniqueRandomNumbers(min, max, count) {
  const values = new Set();

  while (values.size < count) {
    values.add(getRandomInt(min, max));
  }

  return Array.from(values);
}

5. Use crypto APIs for secure values

For passwords, tokens, reset links, or API secrets, do not use Math.random(). Use the Web Crypto API in the browser or the crypto module in Node.js instead.

const values = new Uint32Array(1);
crypto.getRandomValues(values);
console.log(values[0]);

Use the live tool for output

Open the main random number generator when you want immediate browser output, custom ranges, or multiple results without writing code first.

Open the tool

Use the article for depth

Open the long-form article when you want examples, use cases, and explanations of edge cases such as duplicates or security tradeoffs.

Read the article

Use related tools for secure IDs

When the requirement shifts from plain numbers to secure identifiers, move to the random string or UUID tools instead of stretching this pattern too far.

Open random string tool

Frequently asked questions

How do I get a random number between min and max in JavaScript?

Use Math.random() * (max - min) + min for a float, or Math.floor(Math.random() * (max - min + 1)) + min for an integer range.

Is Math.random() secure enough for passwords?

No. For anything security-sensitive, switch to crypto.getRandomValues() in the browser or Node.js crypto on the server.

Where should I go if I want a browser tool instead of a guide?

Use the live random number generator tool. This page is intentionally focused on implementation patterns and code snippets.