RFR 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom

Guy Steele guy.steele at oracle.com
Mon Aug 19 20:21:48 UTC 2013


On Aug 19, 2013, at 4:20 PM, Mike Duigou <mike.duigou at oracle.com> wrote:

> 
> On Aug 19 2013, at 13:12 , Guy Steele wrote:
> 
>> 
>> On Aug 19, 2013, at 3:17 PM, Mike Duigou <mike.duigou at oracle.com> wrote:
>> 
>>> - I find disallowing the zero bounds and empty ranges slightly annoying. It requires me to externally special case for situations such as:
>>> 
>>> Random ran = new Random();
>>> String[] users = {"Fred"}; 
>>> 
>>> someUser = users[mine.nextInt(users.length - 1)];
>>> 
>>> This is a frequently used idiom. Yes, forcing the random number generator to return zero is silly but for symmetry it is convenient. An empty range isn't an obvious error (though the "String[] users = {};" case is obviously an error).
> 
>> If you want a free choice among ALL elements in the (non-empty) array, then
>> 
>>  someUser = users[mine.nextInt(users.length)];
>> 
>> is what you want; and if the array is empty, then you'll get an exception from the nextInt method rather than the array indexing step, but that amounts to the same thing: an inability to pick an element from an empty array.
> 
> Doh, yes. I knew it was important to document that bounds was exclusive--I managed to confuse myself….

That happens to all of us.  :-)

--Guy




More information about the core-libs-dev mailing list