lundi 26 juillet 2021

Generate random numbers between -1 and 1 in java

This is what I am going to do. I want to generate random numbers between -1 and 1. Then I add that generated number to a specific number. I want to continue this until that total value reaches another specific value.

int distance = 5;
int min = -1;
int max = 1;
int randomValue;


   while(distance <= 10){
       randomValue = (int)(Math.random()*(max-min+1)+min);
       distance = randomValue + distance;
       Log.d("Output", "value is"+distance);
   }

This returns values up to 11. how can I resolve this




Aucun commentaire:

Enregistrer un commentaire