I am trying to generate the numbers 1-7 in random order and add them to array. My code is supposed to generate the number, check the number generated to see if it is in the array already, and if not then add the number to the array. The only array location that is not functioning appropriately is n[0]. I have been trying hard to solve the issue but I am stuck, any help would be greatly appreciated. The last 3 times I ran the program I got:
3, 1, 5, 2, 3, 4, 7
6, 1, 3, 7, 5, 2, 6
5, 1, 7, 3, 6, 2, 5
Here is the code to generate a random number
int * randomize()
{
static int n[7];
int i = 0;
int j = 0;
int check = 0;
int check2;
srand((unsigned)time( NULL ));
for( i = 0; i < 7; i++)
{
check = (rand() % 7);
check += 1;
for( j = 0; j < 7; j++)
{
check2 = (int) n[j];
if(check == check2)
{
check = (rand() % 7);
check += 1;
j = 0;
}
}
n[i] = check;
j = 0;
}
return n;
}
And here is the code to call the function from main and print the array
int *r;
r = randomize();
for( i = 0; i < 7; i++){
printf("%i\n", *(r + i));
}