Generate Random Number in Given Range Using JavaScript
Last Updated :
31 Dec, 2024
Improve
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().
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}`);
Output
Random 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.
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);
Output
Random 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.
let min = 1.5;
let max = 5.5;
let random = Math.random() * (max - min) + min;
console.log(`Random decimal between ${min} and ${max}: ${random}`);
Output
Random 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.
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}`);
Output
Random 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.
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));
Output
Unique 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.