mardi 25 août 2015

Is it okay to seed the randomizer more than once if done this way?

Say I have this loop:

for (i=0; i < 100; i++)
{
    srand(time(NULL));
    printf("%d\n", rand());
}

If my machine is reasonably fast, it should print the same number 100 times. But if I modify it like this:

for (i=0; i < 100; i++)
{
    srand(time(NULL) + rand());
    printf("%d\n", rand());
}

Then it should print 100 different numbers. My question is, is there anything wrong in seeding the randomizer like this? What about efficiency? Does it significantly reduce the efficiency?

A side question, if I seed the randomizer in the main() function once, is it enough for other functions as well (i.e. I do not need to seed it again in another function which uses rand())?




Aucun commentaire:

Enregistrer un commentaire