I have been banging my head for two days now trying to make sure the random generator method won't generate a duplicate. This I had to do with loops only without importing any libraries. I have come to this solution, will this code generate a duplicate on the long run? If yes please help.
int [] vargu1 = new int [5];
for ( int i = 0; i < vargu1.length; i++)
{
int numriSekret = (int) (Math.random() * 10)+1;
vargu1[i] = numriSekret;
}
System.out.println(vargu1[i]);
}
System.out.println();
for ( int i = 0; i < vargu1.length; i++){
for ( int j = 0; j < i; j++){
if (vargu1[i] == vargu1[j])
vargu1[i]++;
}
System.out.println(vargu1[i]);
}
Aucun commentaire:
Enregistrer un commentaire