dimanche 30 avril 2017

Generating random numbers in C with different seed in every loop?

Note: This post is not a duplicate of how to generate random numbers in C, it does not answer my question.

I have a function which is:

double generator(double * ran_i) {
  srand(time(NULL));
  return ran_i[rand()%1000];
}

which takes an array containing 1000 numbers, then I try to call:

for(i=0; i< 1000; ++i) {
  printf("%lf", generator(ran[0]));
}

However in that loop it always gives me the same number. I figured out that time(NULL) does not change the seed fast enough. How can I change this seed faster or even better change it in every loop? Also I can only use libraries compatible with ANSI C standard.




Aucun commentaire:

Enregistrer un commentaire