mercredi 12 juillet 2023

Why does the c function srand() behave differently on Windows vs. Linux, where consecutive seeds produce the first consecutive random number?

Here we have a piece of c code that goes into a dead loop, reading the current timestamp and resetting the random seed each time, after which it prints a random number and sleeps for one second. The seeds are consecutive, and Windows generates the first random number consecutively for consecutive seeds, which is not the case with Linux.

Many languages written in c that don't have their own implementation of a random number generator also have this problem, e.g. lua 5.1

#include<time.h>
#include<stdlib.h>
#include<stdio.h>
#ifdef __linux__
#include <unistd.h>
#else
#include <windows.h>
#endif

void printRandoms(int lower, int upper,
                            int count)
{
    int i;
    for (i = 0; i < count; i++) {
        int num = (rand() %
        (upper - lower + 1)) + lower;
        printf("%d\n", num);
    }
}

int main(){
    while(1){
        srand(time(NULL));
        printRandoms(1,10000,1);
        #ifdef __linux__
        sleep(1);
        #else
        Sleep(1000);
        #endif
    }
    return 0;
}

The output:

On Windows:
I tried both gcc and clang.
1. gcc.exe (MinGW-W64 x86_64-ucrt-posix-seh, built by Brecht Sanders) 13.1.0
2. (built by Brecht Sanders) clang version 16.0.5
3. gcc.exe (MinGW-W64 x86_64-msvcrt-posix-seh, built by Brecht Sanders) 9.5.0
5237
5238
5239
5240
5241
5242
5243
5245
5246
5247
5248

On Linux:
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
5142
3660
7270
896
9500
3041
6659
5194
8809
7361
5960

I know that's usually the wrong way to use srand(), but was just curious what led to the difference.




Aucun commentaire:

Enregistrer un commentaire