Home >>JavaScript Math Reference >JavaScript random() Method

JavaScript random() Method

JavaScript random() Method

Javascript Math random() method is used to returns a floating-point pseudo-random number between 0 to led than 1, 0 (inclusive) and 1 (exclusive). This random number then then scale to your desired range.

Syntax:

Math.random();

Parameter Values

Parameter Description
None: This method do not accept any parameter.

Browser Support

Method Chrome Edge Firefox Safari Opera
random() Yes Yes Yes Yes Yes

Here is an Example of JavaScript random () method:

<html>
<body>
<button onclick="myRandom()">click me</button>
<p id="random"></p>
<script>
function myRandom() {
  document.getElementById("random").innerHTML = Math.random();
}
</script>
</body>
</html>
Output:

 

Example 2:

<html>
<body>
<script> 
  var min=10; 
  var max=15; 
  var random = 
  Math.floor(Math.random() * (+max - +min)) + +min; 
  document.write("Random Number Generated : " + random ); 
</script>					 
</body>
</html>
Output:

No Sidebar ads