Seeding rand () in C regardless of time

I am using stdlib to generate random numbers. I know there are better generators, but stdlib is enough for me.

I am doing:

while(condition){
    sleep(1);
    srand(time(NULL));
    r=rand();
}

      

This is inside a loop, so I need sleep(1)

either the seed to be the same and the number will repeat. The thing is, I need to generate thousands or maybe millions of numbers, and if I wait 1 second for a number it will take a long time. So, is there another way, regardless of the time, to sow?

+3


source to share


1 answer


You only need to seed once (at startup) and then generate as many numbers as you like. Don't repeat once per number - it's pointless and you basically need to generate random seeds to generate random numbers (which rather defeats the purpose of using a PRNG in the first place).



+10


source







All Articles