Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript Math random() Method

  • Difficulty Level : Easy
  • Last Updated : 09 Jul, 2020

Below is the example of the Math random() Method.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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




    <script type="text/javascript">
        var random = Math.random( );
        document.write("Random Number Generated : " + random ); 
    </script>
  • Output:
    Random Number Generated : 0.2894437916976895

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

Parameters: This function does not accepts any parameter.



Return Value: The math.random() function returns a floating-point, pseudo-random number between range [0,1) , 0 (inclusive) and 1 (exclusive).

More codes for the above method are as follows:
Program 1: 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:




<script type="text/javascript">
    var min=4;
    var max=5; 
    var random = Math.random() * (+max - +min) + +min;
    document.write("Random Number Generated : " + random ); 
</script>

Output:

Random Number Generated : 4.991720937372939

Program 2: 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 max. For getting a random integer between two values the Math.random() function can be executed in the following way:




<script type="text/javascript">
    var min=4;
    var max=5; 
    var random =
    Math.floor(Math.random() * (+max - +min)) + +min;
    document.write("Random Number Generated : " + random ); 
</script>                    

Output:

Random Number Generated : 4

Program 3: 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:




<script type="text/javascript">
    var min=20;
    var max=60; 
    var random =
    Math.floor(Math.random() * (+max + 1 - +min)) + +min;
    document.write("Random Number Generated : " + random ); 
</script>                    

Output:

Random Number Generated : 60

Supported Browsers:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!