0

can you help me out on how to limit the below to stay within a min and max range eg 3 - 10

<div id="number">3</div> 

  setInterval(function(){
    random = (Math.floor((Math.random()*1)+1));
    var plusOrMinus = Math.random() < 0.5 ? -1 : 1;
    random = random * plusOrMinus; 
    currentnumber = document.getElementById('number');

    document.getElementById('number').innerHTML =  parseInt(currentnumber.innerHTML) + random;

 }, 1000);
manso0161
  • 3
  • 2

1 Answers1

0

See Mozilla Docs:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#getting_a_random_number_between_two_values

Getting a random integer between two values This example returns a random integer between the specified values. The value is no lower than min (or the next integer greater than min if min isn't an integer), and is less than (but not equal to) max.

function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}

By Mozilla Contributors, licensed under CC-BY-SA 2.5.

Lee
  • 29,398
  • 28
  • 117
  • 170