Generating random number in JavaScript

In JavaScript, you can generate a random number using the random() method. This method returns a number between 0 and 1 (including 0).

Generating random number in JavaScript

In JavaScript, you can generate a random number using the random() method. This method returns a number between 0 and 1 (including 0).

Example

<!DOCTYPE html>
<html>
<body>
    <p id="msparagraph"></p>
    <button onclick="msrandom()">Generate random number</button>

    <script>
    function msrandom()
    {
        var p=document.getElementById('msparagraph');   
        p.innerHTML=Math.random();
    }
    </script>

</body>
</html>

Random number between two values


Share Tweet Send
Loading...
You've successfully subscribed to Geekinsta
Great! Next, complete checkout for full access to Geekinsta
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.