Last active
March 13, 2017 19:11
-
-
Save antirez/52529aefb7000aa076f5fc6675252c3c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* MacOS libc PRNG is garbage! | |
* Test this on MacOS 10.12.3 (may be the same with other versions) | |
* You'll see three times the same output. | |
* Cheers by @antirez. */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main(void) { | |
/* Note, 1000000, 1000001, 1000002 are not special. | |
* You can use any other "near" seeds and MOD 7 the sequence will be the same. */ | |
srand(1000000); | |
printf("%d\n", rand() % 7); | |
srand(1000001); | |
printf("%d\n", rand() % 7); | |
srand(1000002); | |
printf("%d\n", rand() % 7); | |
return 0; | |
} |
TLDR: it happens even with this code.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
int seed = time(NULL);
srand(seed+1);
printf("%d\n", rand() % 7);
srand(seed+2);
printf("%d\n", rand() % 7);
srand(seed+10);
printf("%d\n", rand() % 7);
return 0;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@colmmacc read my last post. You misunderstood the point completely. Happens for every possible N seeds that are just similar numbers.