0
How do you generate a random number in the specific java between min and max? Because with the nextInt function of the Random class you can only specify max.
0
How do you generate a random number in the specific java between min and max? Because with the nextInt function of the Random class you can only specify max.
3
You can use the Random
in 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;
}
Browser other questions tagged java random
You are not signed in. Login or sign up in order to post.
And this logic there, has how to explain it? I found interesting this way, but did not understand its functioning.
– user28595
I’m just providing that, @diegofm
– Jéf Bueno
Thanks @jbueno, just move the final result by adding min. Now there is some difference in performance between these two classes?
– Vinicius Fernandes