Generate Random Whole Numbers in JavaScript in a Specific Range



To generate a random number, use the JavaScript Math.random() method. Here set the minimum and maximum value and generate a random number between them as in the following code −

Example

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <script>
         var max = 6
         var min = 2
         var random = Math.floor(Math.random() * (max - min + 1)) + min;
         document.write(random)
      </script>
   </body>
</html>
Updated on: 2020-06-15T05:25:14+05:30

229 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements