Im trying to get random number between -5 and 5. Im using this code:
random.nextInt(10)-5;
but it doesnt give me number 5
and if i use random.nextInt(10)-4;
then I never get -5
.
So how can I get random between -5 and 5?
random.nextInt(11) - 5
You have 11 integers in the interval so you need 11 different values.
The end of the range is exclusive in Random
, so you have to use:
random.nextInt(11) - 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With