Generate Random Number in Given Range Using JavaScript Last Updated : 31 Dec, 2024 Comments Improve Suggest changes Like Article Like Report Here are the different ways to generate random numbers in a given range using JavaScript1. Using Math.random(): basic ApproachThis 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 NumbersWhen 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 NumbersIf 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 RangesTo 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 RepetitionTo 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. Comment More infoAdvertise with us Next Article Generate Random Number in Given Range Using JavaScript divyanshu3035 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-DSA JavaScript-QnA Similar Reads How numbers are stored in JavaScript ? In this article, we will try to understand how numbers are stored in JavaScript. Like any other programming language, all the data is stored inside the computer in the form of binary numbers 0 and 1. Since computers can only understand and process data in the form of 0's and 1's. In JavaScript, ther 6 min read How to create a Number object using JavaScript ? In this article, we will discuss how to create a Number object using JavaScript. A number object is used to represent integers, decimal or float point numbers, and many more. The primitive wrapper object Number is used to represent and handle numbers. examples: 20, 0.25. We generally don't need to w 2 min read How to convert a value to a safe integer using JavaScript ? In this article, we will learn How to convert a value to a safe integer using JavaScript. We have given a number, and we have to convert it into a safe integer using JavaScript. Safe Integers are integers between -(253 - 1) and (253 - 1). Approach: First, we take input using the prompt in JavaScript 2 min read How to add float numbers using JavaScript ? Given two or more numbers the task is to get the float addition in the desired format with the help of JavaScript. There are two methods to solve this problem which are discussed below: Table of Content Using parseFloat() and toFixed() method Using parseFloat() and Math.round() method Using Number() 2 min read Calculate current week number in JavaScript Calculating the current week number involves determining which week of the year the current date falls into. The method of calculation can vary slightly depending on the rules you follow, such as which day starts the week (Sunday or Monday) and how the first week of the year is defined.For example:J 2 min read How to Convert a Float Number to the Whole Number in JavaScript? Given a float number and the task is to convert a float number to a whole number using JavaScript. Below are various methods to convert float numbers to whole numbers in JavaScript:Table of ContentMath.floor (floating argument)Math.ceil (floating argument) Math.round (floating argument)Math.trunc (f 4 min read How to convert Number to Boolean in JavaScript ? We convert a Number to Boolean by using the JavaScript Boolean() method and double NOT operator(!!). A JavaScript boolean results in one of two values i.e. true or false. However, if one wants to convert a variable that stores integer â0â or â1â into Boolean Value i.e. "false" or "true". Below are 2 min read Convert a Number to a String in JavaScript These are the following ways to Convert a number to a string in JavaScript:1. Using toString() Method (Efficient and Simple Method)This method belongs to the Number.Prototype object. It takes an integer or a floating-point number and converts it into a string type.JavaScriptlet a = 20; console.log(a 1 min read Convert a negative number to positive in JavaScript In this article, we will see how we can convert a negative number to a positive number in JavaScript by the methods described below. Below are the methods to convert a negative number to a positive in JavaScript: Table of Content Multiplying by -1Using Math.abs()adding a minus signFlipping the bitUs 4 min read Check a Number is Prime or Not Using JavaScript A prime number is a whole number greater than 1, which has no positive divisors other than 1 and itself. In other words, prime numbers cannot be formed by multiplying two smaller natural numbers. For example:2, 3, 5, 7, 11, and 13 are prime numbers.4, 6, 8, 9, and 12 are not prime numbers because th 5 min read Like