为什么rand()在Linux上重复的次数比Mac重复得多?

I was implementing a hashmap in C as part of a project I'm working on and using random inserts to test it when I noticed that rand() on Linux seems to repeat numbers far more often than on Mac. RAND_MAX is 2147483647/0x7FFFFFFF on both platforms. I've reduced it to this test program that makes a byte array RAND_MAX-long, generates RAND_MAX random numbers, notes if each is a duplicate, and checks it off the list as seen.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

char randoms[RAND_MAX];
int dups = 0;

int main() {
    memset(randoms, 0, RAND_MAX);
    srand(time(0));
    for (int i = 0; i < RAND_MAX; i++) {
        int r = rand();
        if (randoms[r]) {
            // printf("duplicate at %d\n", r);
            dups++;
        }
        randoms[r] = 1;
    }
    printf("duplicates: %d\n", dups);
}

Linux始终生成约7.9亿个副本。 Mac始终只生成一个,因此它遍历几乎可以重复生成的每个随机数。谁能告诉我这是如何工作的?我无法分辨出与手册页不同的东西,无法分辨每个正在使用的RNG,也无法在线找到任何东西。谢谢!

评论