Generate Random Number in Given Range Using JavaScript
Last Updated :
11 Jul, 2025
Here are the different ways to generate random numbers in a given range using JavaScript
1. Using Math.random(): basic Approach
This is the simplest way to generate a random number within a range using Math.random().
JavaScript
let min = 10;
let max = 20;
let random = Math.floor(Math.random() * (max - min + 1)) + min;
console.log(`Random number between ${min} and ${max}: ${random}`);
OutputRandom number between 10 and 20: 19
In this example:
- Math.random() generates a random decimal between 0 (inclusive) and 1 (exclusive).
- Multiply the result by (max - min + 1) to scale it to the desired range, then add min to shift it to the correct starting value.
- Use Math.floor() to round down to the nearest integer, ensuring the result is within the specified range.
2. Generating Multiple Random Numbers
When multiple random numbers are needed, looping simplifies the process.
JavaScript
let min = 10;
let max = 20;
let count = 5;
let random = [];
for (let i = 0; i < count; i++) {
let n = Math.floor(Math.random() * (max - min + 1)) + min;
random.push(n);
}
console.log(`Random numbers between ${min} and ${max}:`, random);
OutputRandom numbers between 10 and 20: [ 18, 13, 14, 19, 16 ]
- Use a for loop to generate count random numbers.
- Store each random number in an array (randomNumbers) for easy access.
3. Random Decimal Numbers
If you need a random decimal (float) within a range, skip rounding.
JavaScript
let min = 1.5;
let max = 5.5;
let random = Math.random() * (max - min) + min;
console.log(`Random decimal between ${min} and ${max}: ${random}`);
OutputRandom decimal between 1.5 and 5.5: 4.300895396340175
- Skip Math.floor() to retain the decimal part.
- Use the same scaling and shifting method to generate decimals within the specified range.
4. Using an Array of Predefined Ranges
To generate random numbers within predefined ranges, arrays can help organize the ranges and randomize the selections.
JavaScript
let ranges = [
{ min: 5, max: 10 },
{ min: 20, max: 25 },
{ min: 30, max: 35 }
];
let range = ranges[Math.floor(Math.random() * ranges.length)];
let n = Math.floor(Math.random() * (range.max - range.min + 1)) + range.min;
console.log(`Random number from selected range (${range.min} to ${range.max}): ${n}`);
OutputRandom number from selected range (5 to 10): 10
- Use an array of objects (ranges) to store multiple ranges.
- Randomly select a range from the array and generate a random number within it.
5. Random Numbers Without Repetition
To generate unique random numbers, use a set to ensure no duplicates.
JavaScript
let min = 10;
let max = 20;
let count = 5;
let unique = new Set();
while (unique.size < count) {
let n = Math.floor(Math.random() * (max - min + 1)) + min;
unique.add(n);
}
console.log(`Unique random numbers between ${min} and ${max}:`, Array.from(unique));
OutputUnique random numbers between 10 and 20: [ 20, 13, 18, 14, 16 ]
- Use a Set to store random numbers, as it automatically prevents duplicates.
- Keep generating numbers until the set contains the required number of unique values.
Explore
JavaScript Basics
Array & String
Function & Object
OOP
Asynchronous JavaScript
Exception Handling
DOM
Advanced Topics