Question

Trying to random an integer between 15-20, inclusive.

Am I implementing this code correctly?

    Random radiusChooser = new Random();
    radius = radiusChooser.nextInt(21)-6;
Was it helpful?

Solution

Try this instead:

radius = radiusChooser.nextInt(6) + 15;

This will choose a random number between 0 and 5 (inclusive) and then add 15 to it - which gives you a random number between 15 and 20 (inclusive).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top