Generate random numbers in JavaScript
In this post, you will learn how to generate a random number in JavaScript.
Such a type of logical question is generally asked in an interview or in competitive exams. This can be helpful in improving your logical programming skills and is helpful in game development and other logical application development.
The JavaScript Math object allows you to perform mathematical tasks on numbers. The Math.random() method returns a random number, but in the range of 0.0 to 1.0. This function only generates floating-point numbers between 0 and 1.
SyntaxMath.random()
It returns a floating-point, pseudo-random number between 0 and 1.
Examples of Math.random
let a = Math.random();
console.log(a);
let b = Math.random();
console.log(b);
Output of the above code:
0.05755110011935449
0.8454330582753098
JavaScript get random number in range(0, 1)
We can use the given formula to find the random value between any two numbers in the range (0,1):
Math.random() * (highestNumber - lowestNumber) + lowestNumber
This given example shows a random floating-point number greater than 1 and less than 10.
// Generating a random number
const x = Math.random() * (10-1) + 1
console.log(`Random value between 1 and 10 is ${x}`);
Output of the above code:
Random value between 1 and 10 is 8.205217479909567
JavaScript get random integer value in range
All the above examples give floating-point random numbers. We can use the Math.floor() method to get a random integer value. It returns the number by decreasing the value to the nearest integer value.
SyntaxMath.floor(Math.random() * (highestNumber - lowestNumber)) + lowestNumber
This given example shows a random integer value between 1 and 10.
// Generating a random integer value
const x = Math.floor(Math.random() * (10-1) + 1)
console.log(`Random value between 1 and 10 is ${x}`);
const y = Math.floor(Math.random() * (10-1) + 1)
console.log(`Random value between 1 and 10 is ${y}`);
Output of the above code:
Random value between 1 and 10 is 4
Random value between 1 and 10 is 2
Integer Value between Two Numbers
If you want to find the random integer in between two specified min and max value, you can use the following formula.
Math.floor(Math.random() * (max - min + 1)) + min
The given program asked the user to enter two min and max values and then used the above formula for generating the random numbers.
// input from the user
const min = parseInt(prompt("Enter a min value: "));
const max = parseInt(prompt("Enter a max value: "));
// generating a random number
const a = Math.floor(Math.random() * (max - min + 1)) + min;
// display a random number
console.log(`Random value between ${min} and ${max} is ${a}`);
Related Articles
Program to find HCF of two numbersProgram to find armstrong number
Program to find prime number
Javascript window location
Star pattern program in Javascript
Number Pattern in Javascript
How to reverse string in Javascript
JavaScript display PDF in the browser using Ajax call
Parsing JSON in Javascript
Javascript speech recognition example
Select/deselect all checkboxes using Javascript
Print specific part of a web page in javascript
Dynamically Add/Delete HTML Table Rows Using Javascript
jQuery Ajax serialize form data example
Image popup on page load using HTML and jQuery
Ajax live data search using jQuery PHP MySQL
jQuery loop over JSON result after AJAX Success
Simple star rating system using PHP, jQuery and Ajax
jquery image zoom on mouseover example
Bootstrap star rating input example
Bootstrap datepicker example
Submit a form data without page refresh