I need to be able to generate a series of ascending or descending numbers within a range.
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);
}
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?
descendingparameter seems redundant, since the caller could simply provide a negative value fordeltawhich would be equally intuitive. Also, mixing the two schemes for restraining into the range (modulo for the top range, clamp at the bottom range) seems odd too.