The JavaScript random function returns the Pseudo-random numbers between Zero and One, and the syntax of this is:
Math.random();
JavaScript random Example
This Function returns the floating point Pseudo-random numbers between 0 to 1.
<!DOCTYPE html> <html> <head> <title>JavaScriptMathRANDOMFunction</title> </head> <body> <h1> JavaScriptMathRANDOM()Function </h1> <p id = "P1"></p> <p id = "P2"></p> <p id = "P3"></p> <p id = "P4"></p> <script> document.getElementById("P1").innerHTML = Math.random(); document.getElementById("P2").innerHTML = Math.random(); document.getElementById("P3").innerHTML = Math.random(); document.getElementById("P4").innerHTML = Math.random(); </script> </body> </html>
The random value generated by this is from the range 0 (included) and less than 1. If you observe the below screenshot, We called the Math function four times, and it returned four different (random) values.
