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>

Updated on: 19-Jun-2020

293 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements