Question: I need a javascript where with every second call i get a random number between 0-100 where current number should be greater than previous number. I wrote a code to get random numbers every second but stuck at generating it with increment.

```
<script>
var span = document.getElementsByTagName("span")[3];
 var i = 100;
(function randNumber() {
        var a = Math.floor((Math.random() * i) + 1);
        span.innerHTML = "RandNumber: " + a;
        setTimeout( randNumber, 1000);
})();

```

Note : The numbers should generate randomly.  

example result may be : `2,5,7,8,22,23,34,56,78,88.....`

Answer: 
You should not create a new random number between zero and your maximum (`i`), but just between the last created number (`lastNumber`) and max (`i`).
Also you might want to stop, when the random numbers reached the maximum.

```
var span = document.getElementsByTagName("span")[3],
    i = 100,
    lastNumber = 0;
function randNumber() {

   lastNumber = lastNumber + Math.floor( Math.random() * (i - lastNumber) + 1 );

   span.innerHTML = lastNumber;

   if( lastNumber < i ) {
     setTimeout( randNumber, 1000 );
   }
}

randNumber();

```

---

AS for the comments and the requirement of a minimum amount of steps until reaching the maximum: 

In each iteration, just increase you number by a random value between `1` and (`(max - min) / steps`. It is pretty much the same code as above.

```
var span = document.getElementsByTagName("span")[3],
    max = 100,
    min = 0,
    lastNumber = 0,
    minSteps = 30;

// how wide can the average step be at most?
var stepWidth = (max - min) / minSteps;

function randNumber() {

   lastNumber = lastNumber + Math.floor( Math.random() * stepWidth + 1 );

   span.innerHTML = lastNumber;

   if( lastNumber < max ) {
     setTimeout( randNumber, 1000 );
   }
}

randNumber();

```
If you extract the "between two numbers" logic into its own function like this, this becomes a lot easier. This way, you just generate a number between your last generated number and your maximum.

```
var max = 100
var last_number = 1

var interval_id = setInterval(function(){
    last_number = get_random_number_greater_between(last_number, max)

    span.innerHTML = "RandNumber: " + last_number;

    if(last_number >= max){
        window.clearInterval(interval_id)
    } 
}, 1000)

function get_random_number_greater_between(low, high){
    return Math.floor(Math.random() * (high - low + 1) + low);
}

```