0

I am trying to output a random number between a range using a function indefinitely. I assume that I use the setInterval() method but I'm confused as to how.

here's my code:

<p id="para"></p>


var randomNumber = function (min, max) {
    return Math.floor(Math.random()*(max-min+1)+(min)); 
}

document.getElementById("para").innerHTML = setInterval(randomNumber(9,15), 5000);

What am I doing wrong here? I want to get a random number between 9 and 15 continuously.

Brixsta
  • 605
  • 12
  • 24
  • `setInterval(randomNumber(9,15), 5000)` **calls** `randomNumber(9,15)` and passes its return value into `setInterval`, exactly the way `foo(bar())` **calls** `bar()` and passes its return value into `foo`. See the linked question's answers for what to do instead. – T.J. Crowder Mar 25 '17 at 09:06

2 Answers2

0
var randomNumberBetween0and19 = Math.floor(Math.random() * 20);
    function randomWholeNum() {
         // Only change code below this line.
    return Math.random();
}
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
0

The first argument for setInterval must be a function, that is getting called, when the interval executes.

In your case, you need a function that overrides your innerHTML with each interval execution.

var randomNumber = function (min, max) {
  return Math.floor(Math.random() * (max-min+1) + (min));
};

var override = function () {
  document.getElementById('para').innerHTML = randomNumber(9, 15);
};

setInterval(override, 5000);