I have a problem about generating random number in C on Windows. In brief, the thing I want to do is to generate 7 numbers that are 0 or 1. Then the function will sum these numbers, result will be the index of global defined array and the value of this index will increase by 1. But in generator function, I always get the same sequence of number. What am I doing wrong?
#include <windows.h>
#include <stdio.h>
#include <time.h>
int cells[8];
int generator(int n) {
int i;
int sum = 0;
for (i = 0; i < n; i++) {
int random_number = rand() % 2 + 0;
printf("%d ",random_number);
sum += random_number;
}
printf("\n%d\n",sum);
return sum;
}
DWORD WINAPI ThreadFunc(void *data) {
cells[generator(7)] =+ 1;
return 0;
}
int main() {
srand(time(NULL));
for (int i = 0; i < 10; ++i) {
HANDLE thread = CreateThread(NULL, 0, ThreadFunc, NULL, 0, NULL);
WaitForSingleObject(thread, INFINITE);
}
for (int j = 0; j < 8; ++j) {
printf("%i: %i\n", j, cells[j]);
}
}