Generate a Random Number in JavaScript Within a Given Range

Robin
Updated on May 14, 2023

There are many uses of random numbers in a program. You can combine Math.random() and Math.floor() methods to generate random numbers in JavaScript within a specific range.

I will show you 3 different techniques for generating random numbers in JavaScript based on specific requirements.

  • Generating a random number within a maximum limit.
  • Generating a random number between X (inclusive) and Y.
  • Generating a random number between X and Y (Both inclusive).

All of these techniques use Math.random() and Math.floor() methods together. You can use these 2 methods in slightly different ways to get the desired outcome.

How to Generate a Random Number in JavaScript Within a Max Limit

The Math.random() function returns a random number between 0 and 1 (not inclusive). When you multiply this number by the desired maximum limit, you will get a random floating point number.

To convert it to an integer, pass the floating number to the Math.floor() method. Finally, it will give you a random integer between 0 and the maximum limit (exclusive).

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

console.log(random)
// 6
        

Suppose your maximum limit is 10 and I want to get a number between 0 to 10. Then multiplying Math.random() by 10 and passing the result to the Math.floor() method will give you an integer between 0 to 9.

Because here 10 isn't inclusive. But if you want to include the maximum limit as a possible random integer, you can simply add 1 to it.

          const random = Math.floor(Math.random() * 10) + 1

console.log(random)
// 10
        

Here adding 1 to the result includes the maximum limit (which is 10 in this example) to the range of possible random integers.

Also Read: How to Generate Random One-time Password (OTP) in JavaScript


Generate a Random Number in JavaScript Between X (Inclusive) and Y

You can generate a random number within a specific range. You will specify minimum (inclusive) and maximum limits, it will give you a random integer between them.

For example, if you set the minimum number as 1 and the maximum number as 10, it will return a random number between 1 to 9.

          const min = 1
const max = 10

const randomNum = Math.floor(Math.random() * (max - min)) + min

console.log(randomNum)
// 7

        

I have defined two constants min and max with values 1 and 10. These variables represent the minimum and maximum limits for our random number.

First, You need to calculate the difference between max and min to determine the range. In this case, the range is (10 - 1) = 9.

Then multiply this range by Math.random(), which generates a random decimal between 0 (inclusive) and 1 (exclusive). Passed the result to Math.floor() to round it down to the nearest integer.

Finally, You will add min to the result to shift the range from starting at 0 to starting at min. This formula generates a random integer between min (inclusive) and max (exclusive).

Also Read: How to Repeat a Task in JavaScript Until Desired Outcome


Generate a Random Number in JavaScript Between X and Y (Both Inclusive)

If you want to generate a random number in JavaScript by giving minimum and maximum limits and want to include both of these numbers in the range of possible random integers, you can use this technique.

For example, if your minimum and maximum values are 1 and 10 respectively, the generated random number can be any integer between 1 and 10, including 1 and 10.

          const min = 1
const max = 10

const randomNum = Math.floor(Math.random() * (max - min + 1)) + min

console.log(randomNum)
// 10
        

Here min and max variables represent the minimum and maximum values. You have to determine the difference between max and min plus 1.

Multiply this result by the random number between 0 to 1 generated by the Math.random() method. You will get a decimal value from this calculation.

Pass that decimal value to the Math.floor() method to round down to the nearest integer.

Finally, You need to add min to the overall result to shift the range to start from 1 instead of 0. This formula will give you a random integer between 1 and 10 (both inclusive).

Also Read: How to Call a Function N Number of Times in JavaScript


Conclusion

You can utilize two simple methods from the Math object in JavaScript to generate a random number. This can be any number within a specific range between two given numbers.

I have shown you 3 different formulas to get random numbers. Each of them will return a slightly different result.

1. Use the following formulas to generate random numbers within a maximum limit.

          // Max limit exclusive
Math.floor(Math.random() * max)

// Max limit inclusive
Math.floor(Math.random() * max) + 1
        

2. Use the following formula to generate random numbers within a specific range – Between X (Inclusive) and Y

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

3. Use the following formula to generate random numbers within a specific range – Between X and Y (Both Inclusive)

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

Now you know all the basics. So you can choose any of them for your projects based on the requirements.

Related Posts