Generate a random integer from range - Javascript Math

Javascript examples for Math:random

Description

Generate a random integer from range

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*from  w w w  . j  av  a 2s  .  c o m*/
var range = "13-50".split("-")
var minlen = parseInt(range[0]);
var maxlen = parseInt(range[1]);

if (!isNaN(minlen) && !isNaN(maxlen))  {
      //Pick a number from range
      length = Math.floor(Math.random() * (maxlen - minlen + 1)) + minlen;
      console.log(length);
}
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials