- You can use
rand() % 9 + 1
to produce random number between 1 and 9.
arr[j]
may be out-of-range after j++;
.
Try this:
int arr[7], j, i;
srand(time(NULL));
arr[0] = rand() % 9 + 1; /* decide first number with special formula */
for (i = 1; i < 7; i++) /* decide the rest numbers */
{
int dupe = 0;
arr[i] = rand() % 10;
for (j = 0; j < i; j++) /* check against numbers that already decided */
{
if (arr[i] == arr[j])
dupe = 1; /* ouch, this number is already used! */
}
if (dupe)
i--; /* if the number is already used, try again */
}
printf("
");
for(j=0;j<7;j++)
printf("
%d ", arr[j]);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…