How to get a pseudo-random number between 0 and 1 in JavaScript?



To get a pseudo-random number between 0 and 1, use the Math.random() method in JavaScript.

Example

You can try to run the following code to get a pseudo-random number between 0 and 1 −

<html>
   <head>
      <title>JavaScript Math random() Method</title>
   </head>
   <body>
      <script>
         var value = Math.random( );
         document.write("First Value : " + value );

         value = Math.random( );
         document.write("<br />Second Value : " + value );
      </script>
   </body>
</html>

Advertisements