1

I started learning JavaScript about a week ago and now I have encountered a problem. My code is not returning proper random number as an answer. Please have a look at my code and help me out. Thanks.

    function calc() {
      var min = document.getElementById('min').value;
      var max = document.getElementById('max').value;
      var x = Math.floor(Math.random() * (max - min) + max);
      document.getElementById('random').innerHTML = x;
    }
<table border='1px' color='black'>
  <tr>
    <td>min</td>
    <td>
      <input type='text' id='min' />
    </td>
  </tr>
  <tr>
    <td>max</td>
    <td>
      <input type='text' id='max' />
    </td>
  </tr>
  <tr>
    <td>
      <input type='button' onClick='calc()' value='generate' />
    </td>
    <td>
      <p id='random'>0</p>
    </td>
  </tr>
</table>

5
  • random() gives a no. between 0 and 1. multiplying it by (max-min) will give us a no between 0 and (max-min) and adding min to both will give a no b/w min and max. this code is generating a random number but now i discovered that it is giving a number between 0 and (max-min) ie it is not taking +min into account :| Commented Jun 8, 2015 at 19:29
  • yes. but how to correct it. Commented Jun 8, 2015 at 19:33
  • max and min are strings, not numbers; use parseInt on them before beginning your computation. Commented Jun 8, 2015 at 19:37
  • "...ie it is not taking +min into account :|" - it's not taking +max into account right?... Commented Jun 8, 2015 at 19:49
  • @War10ck +max is in the OP's code, but it's incorrect. Commented Jun 8, 2015 at 21:17

2 Answers 2

3

You must use parseInt(string,10) on your min and max values before beginning your computation, or else ... + max will concatenate strings instead of adding numbers.

As for the arithmetic, try var x = Math.floor(Math.random() * (max - min + 1) + min) instead.

    function calc() {
      var min = parseInt(document.getElementById('min').value,10);
      var max = parseInt(document.getElementById('max').value,10);
      var x = Math.floor(Math.random() * (max - min + 1) + min);
      document.getElementById('random').innerHTML = x;
    }
<table border='1px' color='black'>
  <tr>
    <td>min</td>
    <td>
      <input type='text' id='min' />
    </td>
  </tr>
  <tr>
    <td>max</td>
    <td>
      <input type='text' id='max' />
    </td>
  </tr>
  <tr>
    <td>
      <input type='button' onClick='calc()' value='generate' />
    </td>
    <td>
      <p id='random'>0</p>
    </td>
  </tr>
</table>

Sign up to request clarification or add additional context in comments.

Comments

1

Change your javascript code like this

    <script type="javascript">              
function calc(){
      var min = parseInt(document.getElementById('min').value);
      var max = parseInt(document.getElementById('max').value);
      var x = Math.floor(Math.random() * (max - min) + min);
      document.getElementById('random').innerHTML = x;    
}                     
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.