The Wayback Machine - https://web.archive.org/web/20250101184038/https://www.geeksforgeeks.org/javascript-math-random-method/
Open In App

JavaScript Math random() Method

Last Updated : 15 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The JavaScript Math.random() function gives you a random number between 0 and just under 1. You can use this number as a base to get random numbers within any range you want. Now, let’s see the syntax to use the JavaScript random method along with multiple examples to easily understand how we can generate random numbers using the random method.

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).

Example 1: For getting a random number between 0(inclusive) and 1(exclusive). 

javascript
let random = Math.random();
console.log("Random Number Generated : " + random);

Output
Random Number Generated : 0.1285732818930101

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);

Output
Random Number Generated : 4.051742762637161

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);

Output
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);

Output
Random Number Generated : 25

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

  • Chrome 51
  • Edge 15
  • Firefox 54
  • Safari 10
  • Opera 38

JavaScript Math random() Method- FAQs

What does the Math.random() method do?

Math.random() returns a floating-point, pseudo-random number in the range 0 (inclusive) up to, but not including, 1 (exclusive).

How does Math.random() work internally?

Math.random() uses a pseudo-random number generator (PRNG) algorithm. The exact algorithm is implementation-dependent and may vary between JavaScript engines.

What are some common uses of Math.random()?

Common uses of Math.random() include generating random numbers for games, simulations, randomized testing, and creating unique identifiers.

Is the distribution of numbers generated by Math.random() uniform?

Yes, the numbers generated by Math.random() are uniformly distributed between 0 (inclusive) and 1 (exclusive), meaning each number in this range is equally likely to be generated.

Is Math.random() truly random?

No, Math.random() is not truly random. It is pseudo-random, meaning it uses an algorithm to generate numbers that approximate randomness. For true randomness, hardware random number generators are required.



Next Article

Similar Reads

three90RightbarBannerImg