Random Numbers JavaScript

<head>
<script>
function getRandomArbitrary(min, max) {

    var r = Math.floor(Math.random() * (max - min + 1) + min);
    document.getElementById('demo').innerHTML = r; 
}
</script>
</head>

<body>
<h1>randomize</h1>
  a: <input type="text" id='low'><br>
  b: <input type="text" id='high'><br>
  <button onclick="getRandomArbitrary(document.getElementById('low').value,document.getElementById('high').value)">Get</button>


<div id='demo'></demo>
</body>

I have above code, through which I am trying to generate random number between range given in texts a and b. when I manually pass the values to this function (say 10,15) it works properly (giving, say 13). but when I press the button and ask it to get value from text and calculate the random number in that range it fails to do so and calculates number which is out of range(sometimes).

I have spent a lot of time on it and couldn’t see what is the problem. Please help me.

6 thoughts on “Random Numbers JavaScript”

  1. 931748 850412Hey. Neat post. There is really a difficulty along with your web site in firefox, and you might want to check this The browser will be the market chief and a large component of other folks will omit your superb writing because of this problem. 383818

    Reply
  2. 327875 979762There several fascinating points in time in this post but I dont know if I see these center to heart. There may possibly be some validity but Ill take hold opinion until I explore it further. Outstanding write-up , thanks and then we want a lot a lot more! Put into FeedBurner too 344974

    Reply

Leave a Comment