title: Java: Generate a sequence of numbers within a range?
body: <p>I need to be able to generate a series of ascending or descending numbers within a range.</p>
  
  <pre><code>public int nextInRange(int last, int min, int max, int delta, boolean descending) {
          delta *= descending ? -1 : 1;
  
          int result = last + delta;
          result %= max;
          return Math.max(result, min);
  }
  </code></pre>
  
  <p>This works fine for ascending values, but not descending values. I've been staring at this for a while, and I'm not sure how to make it work for descending values. Any ideas?</p>