Code generates numbers in array but takes duplicates. I need an array that has 10 elements(numbers from 0 to 9) and I need to use Math.random() method. I thought my code would work but it doesn't and I can't find a mistake.
int[] arrayRnd= new int[10];
for(int i=0; i<=9; i++) {
arrayRnd[i]=(int)(Math.random()*10);
if(i>0) {
for(int j=i-1; j>=0; j--) {
while(arrayRnd[i]==arrayRnd[j]) {
arrayRnd[i]=(int)(Math.random()*10);
}}}}
I expect an output with randomly positioned elements of array for example [6,2,4,1,9,0,3,8,5,7] without duplicates. My output is [1, 7, 3, 8, 5, 6, 4, 5, 7, 2]
Aucun commentaire:
Enregistrer un commentaire