On 15 June 2013 14:58, Duncan Jones <dun...@wortharead.com> wrote: > On 14 June 2013 07:55, Duncan Jones <dun...@wortharead.com> wrote: >> On 13 June 2013 21:26, Phil Steitz wrote: >>> On 6/13/13 1:03 AM, Duncan Jones wrote: > <snip> >> >>> I am not sure where this little method would fit in [lang]; but it might >>> make sense to add there in the simple form above (with RANDOM >>> static, I assume). >> >> Indeed, I had assumed any addition to [lang] would just use a standard >> source of randomness (i.e. a static Random or SecureRandom instance). >> However, I'm struggling to see an existing class that could >> accommodate the functionality and creating a new "RandomUtils" class >> feels a bit heavyweight, unless others can think of additional methods >> that would be useful. > > I gave it some further thought and realised there were some other > methods that would prove useful in such a "RandomUtils" class. I've > created https://issues.apache.org/jira/browse/LANG-900 and attached an > implementation. I welcome comments.
In particular, I welcome comments on whether or not to constrain the start and end range values to be non-negative. In the current implementation (attached to the bug), I enforced non-negative values, e.g. for `nextInt(X, Y)`, both X and Y must be non-negative. This simplified the implementation because it meant that "Y - X" couldn't be larger than the maximum value of an int. However, this could be changed for nextInt with (untested) code such as: public static int nextInt(int startInclusive, int endExclusive) { Validate.isTrue(endExclusive >= startInclusive, "Start value must be smaller or equal to end value."); if (startInclusive == endExclusive) { return startInclusive; } long range = endExclusive - startInclusive; return (int) startInclusive + nextLong(0, range); } This may work for ints, but it gets more difficult for longs. Any thoughts on this? Duncan --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org For additional commands, e-mail: dev-h...@commons.apache.org