How to Create Random Numbers in JavaScript

How-to basics · Updated November 23, 2026

To create a random number in JavaScript, the core tool is Math.random(), which returns a floating-point number between 0 (inclusive) and 1 (exclusive). From there you can scale and shift it to get integers or decimals in any range you need. This guide covers the essential patterns, common pitfalls, and practical examples.

What You’ll Need

  • A web browser with developer tools (press F12 or right-click > Inspect) or any environment that runs JavaScript, like Node.js.
  • Basic familiarity with JavaScript syntax (variables, functions, console).

Step-by-Step

  1. Generate a basic random decimal. Call Math.random() to get a number between 0 and just under 1. For example, open your browser console and type console.log(Math.random()). You’ll see something like 0.732. This is your foundation.
  2. Scale to a range. Multiply the result by the size of the range you want. To get a number between 0 and 10, use Math.random() * 10. This gives a float like 7.123.
  3. Shift the range. Add a starting value to move the range. For a number between 5 and 15, first calculate the range size (15 – 5 = 10), then do Math.random() * 10 + 5. This yields values from 5 up to (but not including) 15.
  4. Get a random integer. Use Math.floor() to round down to the nearest whole number. For a random integer between 0 and 9, use Math.floor(Math.random() * 10). The * 10 produces 0 to 9.999…, and Math.floor() turns that into 0–9.
  5. Create a reusable function. Write a function that takes a minimum and maximum (inclusive) and returns a random integer. Here’s a standard implementation:
    function getRandomInt(min, max) {
      min = Math.ceil(min);
      max = Math.floor(max);
      return Math.floor(Math.random() * (max - min + 1)) + min;
    }

    This works because max - min + 1 gives the number of possible values, and adding min shifts the result. For example, getRandomInt(1, 6) simulates a die roll.

  6. Handle non-integer ranges. If you need a random float between two numbers, use Math.random() * (max - min) + min. For a random decimal between 1.5 and 5.5, do Math.random() * 4 + 1.5.
  7. Use a cryptographically secure generator when needed. For security-sensitive applications (like password generation or gambling), Math.random() is not suitable because it’s not cryptographically secure. Instead, use the Web Crypto API: crypto.getRandomValues(). Here’s an example for a random integer between 0 and 255:
    const arr = new Uint8Array(1);
    crypto.getRandomValues(arr);
    const randomInt = arr[0]; // 0 to 255

    In Node.js, you can use crypto.randomInt() for integers.

Common Mistakes

  • Forgetting that Math.random() never returns 1. If you use Math.round() instead of Math.floor(), the maximum value becomes twice as likely as others. For example, Math.round(Math.random() * 10) gives 0–10, but 0 and 10 each have half the probability of the numbers 1–9. Use Math.floor() for uniform distribution.
  • Off-by-one errors. If you want numbers from 1 to 10 inclusive, using Math.floor(Math.random() * 10) + 1 is correct. But if you mistakenly use * 11, you’ll get 1–11, which is wrong.
  • Using Math.random() for security. It’s predictable and can be exploited. Never use it for passwords, tokens, or any security-related randomness.
  • Assuming the range is inclusive on both ends. By default, Math.random() gives an exclusive upper bound. If you need the maximum to be included, you must add 1 to the multiplier and then floor.

Tips

  • Use a helper function for clarity. Instead of repeating the formula, define a function like randomInt(min, max) and reuse it. This reduces errors.
  • Remember that Math.random() is not seedable. You cannot set a seed for reproducible results. For simulations that need reproducibility, consider a custom pseudo-random number generator (PRNG) like a linear congruential generator, but for most web tasks Math.random() is fine.
  • For arrays, use Math.random() to pick a random element: arr[Math.floor(Math.random() * arr.length)].
  • To shuffle an array, use the Fisher-Yates algorithm with Math.random(); it gives a uniform shuffle.
  • When generating many random numbers, performance is rarely an issue with Math.random(), but if you need millions, consider using a generator like mulberry32 for speed.

Troubleshooting

  • My random numbers are always the same. That’s not possible with Math.random() in a normal environment; it’s seeded by the system. If you’re using a custom PRNG with a fixed seed, that’s expected. If you’re seeing the same value repeatedly in a loop, check that you’re calling Math.random() inside the loop, not once before it.
  • I get a negative number. This happens if you subtract instead of add the minimum. For example, Math.random() * 10 - 5 gives -5 to 5. If you want 5 to 15, use + 5.
  • I get a decimal when I wanted an integer. You forgot to use Math.floor(). Wrap the expression with Math.floor() or use Math.trunc() for positive numbers.
  • My function returns a value outside the range. Double-check your min/max arithmetic. For inclusive integers, the formula is Math.floor(Math.random() * (max - min + 1)) + min. If you use max - min without the +1, the max is never returned.
  • In Node.js, crypto.getRandomValues is not defined. That’s a browser API. Use crypto.randomInt(min, max) from the built-in crypto module instead.

Frequently Asked Questions

  • Can I generate a random number between 1 and 100? Yes. Use Math.floor(Math.random() * 100) + 1 for an integer from 1 to 100 inclusive.
  • How do I generate a random decimal between two numbers? Use Math.random() * (max - min) + min. For example, Math.random() * 9 + 1 gives a number from 1 to just under 10.
  • Is Math.random() truly random? No, it’s a pseudo-random number generator. It’s deterministic but appears random. For most non-security purposes, it’s sufficient.
  • How can I make the random number unpredictable for security? Use the Web Crypto API: crypto.getRandomValues() in browsers, or crypto.randomBytes() in Node.js. These are cryptographically secure.
  • Can I set a seed to get the same sequence every time? Math.random() doesn’t support seeding. You can implement your own seeded PRNG, like a simple LCG, if you need reproducibility.
  • Why do I sometimes get 0 but never get the maximum? That’s because Math.random() returns values from 0 up to but not including 1. When you multiply by the range size and floor, the maximum is never reached unless you add 1 to the multiplier.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.