I am trying to generate 7 numbers in C. The code should contain:
- It mustn't start with 0.
- The numbers must be between 0-9
- Numbers must be different from each other. (For example: there can't be two 5s, like this one: 7 5 8 3 2 5 4).
My code is working. It doesn't start with 0. Numbers are randomly generated between 0-9.
But I can't manage to include third thing. Same numbers are coming when I start the code. Do you know how can I generate all differently?
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <locale.h>
#include <time.h>
int main() {
srand(time(NULL));
int numbers[10];
int i;
for (i = 0; i < 7; i++) {
numbers[i] = rand() % 10;
if (numbers[0] == 0) {
numbers[0] = 1 + rand() % 9;
}
printf(" %d ", numbers[i]);
}
getch();
return 0;
}