mardi 4 mai 2021

receiving two different, random elements of a list without deleting | Flutter/Dart

Community! I got a problem and might need your help:

so, I got a list List<String> name = ['Max', 'Easter', 'Egg']; and want to generate two random numbers to get a random String of the List.

var rnd = new Random();
int player1;
int player2;

void rndnbrs() {
setState(() {
    player1 = rnd.nextInt(name.length);
    player2 = rnd.nextInt(name.length);
  }
}

So, now I can use name[player1] and name[player2] in Text()

I want player1 and player2 to be different numbers. Thought about deleting element name.removeAt(player1) after generating player1, and adding it right after generating player2 back to the List, but thats not possible (would be a waste of time for you, so I won´t explain why). You guys got any ideas? I´m new to flutter, so there may be a simple solution that I just don´t know about :)




Aucun commentaire:

Enregistrer un commentaire