A number generated by a process, whose outcome is unpredictable is called Random Number. In JavaScript, this can be achieved by using Math.random() function. This article describes how to generate a random number using JavaScript.
Method 1: Using Math.random() function: The Math.random() function is used to return a floating-point pseudo-random number between range [0,1) , 0 (inclusive) and 1 (exclusive). This random number can then be scaled according to the desired range.
Syntax:
Math.random();
Example 1: This example generate an integer random number between 1(min) and 5(max).
<script> // Function to generate random number function randomNumber(min, max) { return Math.random() * (max - min) + min; } document.write("Random Number between 1 and 5: ") // Function call document.write( randomNumber(1, 5) ); </script> |
Output:
Random Number between 1 and 5: 1.0573617826058959
Method 2: Using Math.floor() function: The Math.floor() function in JavaScript is used to round off the number passed as parameter to its nearest integer in Downward direction of rounding i.e towards the lesser value.
Syntax:
Math.floor(value)
Example 2: This example generate random integer number between 1(min) and 100(max).
<script> // Function to generate random number function randomNumber(min, max) { return Math.floor(Math.random() * (max - min) + min); } document.write("Random Number between 1 and 100: ") // Function call document.write( randomNumber(1, 100) ); </script> |
Output:
Random Number between 1 and 100: 87
Example 3: This example generate random whole number between 1(min) and 10(max) both inclusive.
<script> // Function to generate random number function randomNumber(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min; } document.write("Random Number between 1 and 10: ") // Function call document.write( randomNumber(1, 10) ); </script> |
Output:
Random Number between 1 and 10: 3
Recommended Posts:
- PHP program to Generate the random number in the given range (min, max)
- How to generate a random boolean using JavaScript ?
- How to Generate a Random Password using JavaScript ?
- JavaScript to generate random hex codes of color
- Generate random alpha-numeric string in JavaScript
- Program to generate random string in PHP
- How to generate a random, unique, alphanumeric string in PHP
- How to generate a n-digit number using JavaScript ?
- How to pick a random color from an array using CSS and JavaScript ?
- JavaScript Math random() Method
- JavaScript | Random
- How to select a random element from array in JavaScript ?
- How to create an array with random values with the help of JavaScript ?
- JavaScript | Program to generate one-time password (OTP)
- Generating Random String Using PHP
- How to generate a drop down list of timezone using PHP ?
- How to generate an XML file dynamically using PHP?
- How to Generate fake data using Faker module in Node.js ?
- Creating A Range Slider in HTML using JavaScript
- How to get random value out of an array in PHP?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


