0

Do you have an idea how to generate a random Object until an 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 sound good. Any advice ?

lancegerday
  • 752
  • 1
  • 8
  • 24
  • 2
    Rather than possibly re-pick the same objects multiple times, simply shuffle the array and try objects in order until you find you want one or find none – xtratic Jun 09 '20 at 17:35
  • Please, can you write even in "word-code" how do you do that? :) – whyAlwaysMe Jun 09 '20 at 18:10

1 Answers1

0

try do-while loop

verifyMyCondition(){
   //proccess and return true or false
}

do
{
     randomName = this.getTeam1().keySet().toArray()[new Random().nextInt(this.getTeam1().keySet().toArray().length)];
}while(verifyMyConditionthis.getTeam1().get(randomName)))
talevineto
  • 51
  • 4