mardi 9 juin 2020

how to generate random one indefenitly till it respects a condition in Java?

Do you have an idea how to generate a random Object till it this object doesn't respect a condition ?

I tried like this :

//Btw getTeam1() is a hashMap of Integer,Fighter (a type of objects that I use)
//randomIndex
Object randomName = this.getTeam1().keySet().toArray()[new Random().nextInt(this.getTeam1().keySet().toArray().length)];
while(this.getTeam1().get(randomName).dontVerifyMyConditionBlabla) {
        randomName = this.getTeam1().keySet().toArray()[new Random().nextInt(this.getTeam1().keySet().toArray().length)];
}

But apparently, it doesn't sounds good. Any advice ?




Aucun commentaire:

Enregistrer un commentaire