This question already has an answer here:
- Creating random numbers with no duplicates 15 answers
Random r = new Random();
int a[] = new int[16];
for (int i = 0; i < a.length; i++ ) {
for( int j = 0; j < a.length; j++) {
a[j] = (int)(Math.random()* 16);
}
}
System.out.println(Arrays.toString(a));
This code takes an array with the size 16 and fill it with random numbers. When I print the array, I get duplicates of the same number. I don't want to have duplicates. I would like to remove them and have them replace with another number not used. Can anyone help?
Aucun commentaire:
Enregistrer un commentaire