I'm trying to generate a 5x20 matrix filled with random numbers. how can I make sure none of the random numbers are duplicates? this is the code I have for filling the matrix with random numbers.
srand(time(0));
int matrix[5][20];
int i = 0;
int j = 0;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 20; j++)
{
matrix[i][j] = 1 + (rand() % 100);
cout << matrix[i][j] <<"_";
}
cout << endl;
}
the code works but there are sometimes duplicates. if this were an array I could make use of a simple for loop and compare all of the elements in the array. but I have no idea how to do so with a matrix. I have searched wvrywhere but cant seem to find a solution.
Aucun commentaire:
Enregistrer un commentaire