Open In App

JavaScript Random

Last Updated : 03 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

JavaScript Random refers to the Math.random() function, which generates a pseudo-random floating-point number between 0 (inclusive) and 1 (exclusive). This function is commonly used for tasks requiring randomization, such as games, simulations, and cryptographic applications.

Syntax:

Math.random()

Return Value: The Math.random() method returns a value less than 1.

Example: This example uses Math.random() function to return a random number. 

html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    <title>Random Number Example</title>
</head>

<body>
    <h2>Math.random()</h2>

    <p>
        Math.random() returns a random
        number between 0 and 1
    </p>

    <p id="GFG"></p>

    <!-- Script to use Math.random() function -->
    <script>
        document.getElementById("GFG").innerHTML
            = Math.random();
    </script>
</body>

</html>

Output:

 

Random Integers: The Math.random() method is used with Math.floor() function to return random integers. 

Example: This example generated a random number every time we run the code.

html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    <title>Random Number Example</title>
</head>

<body>
    <center>
        <p>
            Math.floor(Math.random() * 11) returns
            a random integer between 0 and 10
            (inclusive):
        </p>

        <p id="demo"></p>
    </center>

    <!-- Script to use Math.random() function
        and Math.floor() function to return random
        integer -->
    <script>
        document.getElementById("demo").innerHTML =
            Math.floor(Math.random() * 11);
    </script>
</body>

</html>

Output:

  

Note: The output of the code varies every time we run the code.

Proper Random Function: The javaScript function always returns a random number between min and max where min is inclusive and max may be inclusive or exclusive. 

Example: This example generates a proper random number.

html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    <title>Random Number Generator</title>
</head>

<body>
    <p>
        When you click the button, it will
        give a random value between 0 and
        9 (both inclusive):
    </p>

    <button onclick="document.getElementById('GFG').innerHTML =
                    getRndInteger(0, 10)">Try it</button>

    <p id="GFG"></p>

    <!-- Script to return random number between
            0 and 10 -->
    <script>
        function getRndInteger(min, max) {
            return Math.floor(Math.random() * (max - min + 1)) + min;
        }
    </script>
</body>

</html>

Output:

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: 



Next Article

Similar Reads