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

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

Below are some examples of using Math.random() in JavaScript to generate random numbers in different ranges:

  • Example 1: For getting a random number between 0(inclusive) and 1(exclusive),the following code can be executed:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    Random Number Generated : 0.2894437916976895
  • 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    Random Number Generated : 4.991720937372939
  • 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 max.

    For getting a random integer between two values the Math.random() function can be executed in the following way:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    Random Number Generated : 4

Supported Browsers: The browsers supported by JavaScript Math.random() function are listed below:

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


My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

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.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.