Pages

Friday, 6 October 2017

Javascript Random Number


Math.random function is use to generate random number. It generates a long series of number less than one or zero. To get random number from a range you need to extract numbers as your requirement by round-off.



Following code demonstrate to generate number from 1 to 100.

<html>
<head>
     <title>Girfa Student Help : Random Number</title>
     <script>
          function getRandom()
          {
              alert(Math.random());
          }
          function getRandomRange()
          {
              alert(parseInt(Math.random()*100));
          }
     </script>
</head>
<body>
<h1 align="center">Random Number </h1><hr />
<input type="button" value="Generate" onclick="getRandom()" />
<input type="button" value="Between 1 to 100" onclick="getRandomRange()" />
</body>

No comments:

Post a Comment