0

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]);
    }
}
Gil Hamilton
  • 11,973
  • 28
  • 51
Zeevac
  • 39
  • 1
  • 6
  • 3
    Impressive complexity! :-) Also, in line `cells[generator(7)] =+ 1;` you probably meant to use `+=`. – Chris Olsen Mar 18 '19 at 22:46
  • https://stackoverflow.com/questions/52884311/generating-the-same-random-numbers-with-threads-in-omp/52885163#52885163 is basically the same problem, and I'm too lazy to answer it again. – nemequ Mar 18 '19 at 22:49
  • See [What does `=+` mean in C?](https://stackoverflow.com/questions/7573978/what-does-mean-in-c/) – Jonathan Leffler Mar 18 '19 at 23:39
  • Guys, I corrected that mistake but it isn't the problem I have. My problem is every call of the function, it prints same values. For example; first iterative in loop: 1 1 0 0 1 1 1. Second iterative :1 1 0 0 1 1 1 and so on. It always gives same values. – Zeevac Mar 18 '19 at 23:49

2 Answers2

1

Apart from "=+ 1" issue... there is an additional concern with the code:

Microsoft explicitly says that the seed is thread-specific (see https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/srand):

The srand function sets the starting point for generating a series of pseudorandom integers in the current thread.

That implies that the seed value (/ sequence generator) is saved in thread-local storage. However, they don't specify what happens if you call srand before creating additional threads. It seems quite likely to me therefore that the thread-local storage containing the seed value will either be copied from the initializing thread - or it will simply be reinitialized with the default seed in each thread the first time you call rand as if you hadn't called srand at all.

In either case, all threads would end up generating the the same pseudo-random sequence anyway.

Hence, you will be better off calling time once, adding the thread index to that initial time value, passing the result as an argument to the thread function, then using that (thread-specific) value as the argument to srand within the thread. At least then each thread will start from a different initial seed value.

Gil Hamilton
  • 11,973
  • 28
  • 51
0

You don't need a thread-safe random number. Your code always calls rand in a precise deterministic sequence because you wait for each thread to finish before starting the next one. Your problem is this;

cells[generator(7)] =+ 1;

This sets the cell to +1 rather than adding one to it.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278