Search code examples
csrand

C program - srand()


Possible Duplicate:
Recommended way to initialize srand?

I have the following problem when using srand() in c.

I call srand(time(NULL)) in a loop, but the loop finishes before 1 sec and every time I call the rand() I get the same value.

How can I solve this?


Solution

  • because the seed is bound into the time() which are seconds since unix epoch, basically you're giving it the same seed because the loop takes less than a second.

    What you should do is get the time in microseconds. Take a look at gettimeofday() if you're coding for windows google microseconds win32 C, you mind need to convert it from double to integerso just do this (unsigned int)double * 100.0f;