Math.random

The Math object allows you to perform mathematical tasks on numbers and includes several mathematical methods. One common use of the Math object is to create a random number with Math.random(), which always returns a number lower than 1:

   Math.random();   // returns a random number

HTML file: Displayed by browser:
<body>
<p>Math.random() returns a random number between 0 (inclusive) and 1 (exclusive):<p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
    document.getElementById("demo").innerHTML = Math.random();
}
</script>
</body>

Math.random() returns a random number between 0 (inclusive) and 1 (exclusive):

Unlike other global objects, Math has no constructor, nor is a constructor. All the properties and methods of Math are static and can be called by using Math as an object without creating it. In other words, no methods have to create a Math object first.

BackTable of ContentsNext