Domanda

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

Am I implementing this code correctly?

    Random radiusChooser = new Random();
    radius = radiusChooser.nextInt(21)-6;
È stato utile?

Soluzione

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).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top