Question: Let's say I need to generate 100 numerical values. The value that is generated each time must be greater than or equal to the previous generated value. The issue is is that each individual value MUST be generated on the spot when needed, so we do not store any of the data. I have a solution using recursion, but the issue is is that if I want to generate the 100th value, it will have to recursively go through and generate and check all of the previous values, which you can imagine is terrible. The issue is further complicated by the fact that, if I'm generating the 100th value, I don't know for sure that the 99th value is accurate. Perhaps the 99th value is smaller than the 98th value, so the 99th value must do that check - so when generating the 100th value, we must not only check the 99th value, but potentially the 98th value, etc... Is there any way of solving this more eloquently without generating and storing all of the data before hand? Thank you!

Answer: 
> 
> The value that is generated each time must be greater than or equal to the previous generated value.
> 
> 
> 

Just generate a random displacement from the previous value; like:

```
    next_value = previous_value + get_random_value_from_zero_to_whatever();
    previous_value = next_value;

```

The problem you need to worry about (regardless of how you do it) is exhaustion - what happens when the previous value was the highest possible value (e.g. `INT_MAX` if you're using `int`). Do you continually use the same highest possible value with no randomness at all (to avoid a smaller value), or...?
You actually only have to store one value, not all of them. The proof is by induction:

* If you generate a value and it's the very first time, that value is trivially greater or equal to all previous
* If you know the previously generated value *v*, which by induction is greater or equal to all previous values, then you merely have to generate a value larger than *v* for it to be greater or equal to all previous

You could also just generate 100 values and then sort them.