JavaScript Math random() Method
Javascript 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();
Parameters: This function does not accept any parameter.
Return Value: The math.random() function returns a floating-point, pseudo-random number between range [0,1) , 0 (inclusive), and 1 (exclusive).
Below is an example of the Math random() Method.
Example 1: For getting a random number between 0(inclusive) and 1(exclusive).
javascript
let random = Math.random();console.log("Random Number Generated : " + random); |
Random Number Generated : 0.8781937444293155
Example 2: Math. random() can be used to get a random number between two values. The returned value is no lower than min and may possibly be equal to min, and it is also less than and not equal to max. For getting a random number between two values the math.random() function can be executed in the following way:
Javascript
let min = 4;let max = 5;let random = Math.random() * (+max - +min) + +min;console.log("Random Number Generated : " + random); |
Random Number Generated : 4.895468300181781
Example 3: Math.random() can be used to get an integer between two values. The returned value is no lower than min or it is the next integer greater than min if min isn’t an integer. It is also less than but not equal to the max. For getting a random integer between two values the Math.random() function can be executed in the following way:
javascript
let min = 4;let max = 5;let random = Math.floor(Math.random() * (+max - +min)) + +min;console.log("Random Number Generated : " + random); |
Random Number Generated : 4
Example 4: Math.random() can be used to get an integer between a minimum and a maximum value, including not only the minimum but also the maximum. For getting a random integer between two values the Math.random() function can be executed in the following way:
javascript
let min = 20;let max = 60;let random = Math.floor(Math.random() * (+max + 1 - +min)) + +min;console.log("Random Number Generated : " + random); |
Random Number Generated : 49
We have a complete list of Javascript Math Objects methods, to check those please go through this Javascript Math Object Complete reference article.
Supported Browsers:
- Google Chrome 1 and above
- Firefox 1 and above
- Opera 3 and above
- Safari 1 and above
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!

