How do you generate a random number in the specified java between min and max? Because with the nextInt function of the Random class you can only specify the max.
How do you generate a random number in the specified java between min and max? Because with the nextInt function of the Random class you can only specify the max.
You can use Random
this way
public int numeroAleatorio(int min, int max){
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
Or use the class Math
public int numeroAleatorio(int min, int max){
int randomNum = min + (int)(Math.random() * (max - min));
return randomNum;
}