jeudi 7 mars 2019

Is there a better method for randomizing functions besides Random?

I have 2 strings in an array. I want there to be a 10% chance of one and 90% chance to select the other. Right now I am using:

Random random = new Random();
int x = random.nextInt(100 - 1) + 1;
if (x < 10) {
    string = stringArray(0);
} else {
    string = stringArray(1);
}

Is this the best way of accomplishing this or is there a better method?




Aucun commentaire:

Enregistrer un commentaire