How to generate random numbers between two numbers in JavaScript?

JavascriptWeb DevelopmentFront End Technology

To generate random numbers between two numbers, use Math.random() method.

Example

You can try to run the following code to get random numbers between a minimum and maximum number −

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <script>
         function randomInt(min, max) {
            min = Math.ceil(min);
            max = Math.floor(max);
            return Math.floor(Math.random() * (max - min)) + min;
         }
         alert(randomInt(5,25));
      </script>
   </body>
</html>
raja
Published on 03-Feb-2018 16:40:23
Advertisements