Math.random() : random « Math « JavaScript Tutorial






Syntax

math.random(num)

The random() method of the Math object is used to obtain a random number between values 0 and 1.

<html>
    <body>
    <script language="JavaScript">
    <!--
    function doMath(){
        var result = Math.random();
        document.form1.answer.value = result;
    }
    -->
    </script>
    <form name=form1>
    The random number is:
    <input type="text" name="answer" size=20>
    <input type="button" value="Calculate" onClick='doMath()'>
    <br>
    </form>
    </body>
    </html>








9.21.random
9.21.1.Math.random()
9.21.2.Use Math.random to generate random number
9.21.3.Math.round a random number
9.21.4.Generate a random integer between 2 and 10
9.21.5.Generate random URL
9.21.6.Generate random numbes in a range