0

This is my code:

<!DOCTYPE html>
<html>
  <head>
    <title>Random Number Generator</title>
  </head>
  <body>
    <h1>
      Random Number Generator
    </h1>
    <p>
      Minimum:
      <input type="number" id="min" />
    </p>
    <p>
      Maximum:
      <input type="number" id="max" />
    </p>
    <button onclick="go()">
      Go
    </button>
    <p id="ran"></p>
    <script>var min, max;
function go() {
  min = document.getElementById("min").value;
  max = document.getElementById("max").value;
  document.getElementById("ran").innerHTML = Math.floor(
    Math.random() * (max - min + 1) + min
  );
}
</script>
  </body>
</html>

It is a random number generator. My problem is that the result is less than the minimum. I'm positive that the problem is with the input because one time I replaced max and min with numbers and it came up with a number between them.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
codey
  • 1
  • 1
    use `min = document.getElementById("min").valueAsNumber` – Mister Jojo Feb 27 '21 at 20:35
  • `Math.floor( Math.random() * (max - min + 1) + min );` should be `Math.floor( Math.random() * (max - min + 1) ) + min;` - you need to multiply -> round down the multiplication result -> *then* add the minimum. Not multiply -> add minimum -> round down the whole result. – VLAZ Feb 27 '21 at 20:35
  • Thanks, ```document.getElementById("min").valueAsNumber``` worked. – codey Feb 28 '21 at 16:41

0 Answers0