Math random() Method - Javascript Math

Javascript examples for Math:random

Description

The random() method returns a random number from 0 up to but not including 1.

Parameters

None

Return Value:

A Number, representing a number from 0 up to but not including 1

The following code shows how to return a random number between a range:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {// w w w  . j a  v a2  s  .c o m
    var x = document.getElementById("demo")
    x.innerHTML = Math.floor((Math.random() * 100) + 1);
}
</script>

</body>
</html>

Related Tutorials