jeudi 25 août 2016

How to specify ranges to java.util.Random?

I don't know why Java's designers didn't design java.util.Random objects to return random numbers like this:

rand.nextInt(minValue, maxValue);

I don't understand how to pass min and max values to this function. I've seen this recommended:

random.nextInt(max + 1 - (min)) + min

but what does that mean? Does that mean:

random.nextInt(FULL_RANGE + 1) + START_OF_RANGE

For example, if I wanted to generate values between -1 and 1, inclusive, is this how I do it:

int oneOfThreeChoices = rand.nextInt(1 + 1 + 1) - 1;




Aucun commentaire:

Enregistrer un commentaire