How to generate random whole numbers in JavaScript in a specific range?

JavascriptWeb DevelopmentFront End Technology

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>
raja
Published on 18-Jan-2018 08:43:21
Advertisements