I have tried to come up with a 2-D (4 rows and 4 columns) array which holds values from 0 to 15, with none of the values used more than once.
I'm posting only the code snippet.
please lemme know if the logic is correct.
int HIGH=16; /*(Max value of an array element)+1*/
int done[15]; /* Shows which of the 16 numbers have already been used*/
int i, j, val;
time_t secs;
time(&secs); /*assign 'secs' with the current time*/
srand((unsigned)secs); /*initialize the seed for rand()*/
for(i=0;i<15;i++)
done[i]=0; /* None of the 16 numbers have been used yet*/
for(i=0;i<4;)
{
for(j=0; j<4;)
{
val=rand()%HIGH; /*generates a number between 0 and 15*/
if(!done[val]) /*if number hasn't been used already*/
{
a[i][j]=val;
done[val]=1; /* mark the particular number as done*/
j++; /* increment j only if the current array position gets assigned a number*/
}
}
i++;
}