lundi 4 juillet 2022

List of random numbers without repetition

Why does the first code generate a list of random numbers with repetition and the second code a list of numbers without repetition? The difference is only a list declared with int[] in (1) and int{} in(2).

1.)

final generatedRandoms = <int>[];

final rng = Random();
while (generatedRandoms.length < 100) {
  final gr = rng.nextInt(100) + 1;
  generatedRandoms.add(gr);
}

2.)

final generatedRandoms = <int>{};

final rng = Random();
while (generatedRandoms.length < 100) {
  final gr = rng.nextInt(100) + 1;
  generatedRandoms.add(gr);
}



Aucun commentaire:

Enregistrer un commentaire