mercredi 13 mai 2015

C: srand does not influence random number generater

I have the following c code:

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

int main(int argc, char *argv[]) {
  srand(time(NULL));
  printf("%d\n", (int)random());
  return 0;  
}

To my understanding, this should print a different random number every time I execute the program because the random seed is dependent of the system time.

But every time run the program, I get exactly the same output:

1804289383

I still get the same output when I put custom values as argument for srand:

srand(1);

Or

srand(12345);

Does anyone have an Idea why this happens? Maybe it is because of my operating system (Mac OS 10.10.3)? Or the compiler I use (gcc)?

Are there simple alternatives?




Aucun commentaire:

Enregistrer un commentaire