Search code examples
crandomsrand

using the time function for srand in pseudo-random number generator in c


#include <time.h>
((unsigned)time(NULL));

I don't understand why you have to include the time header file and use the time function in your program when creating a pseudo-random number generator using the function srand(). Can somebody please explain what the significance of time is in this case?

* Please note that the code shown is only part of the program. Thank you.


Solution

  • It makes the code non-repeatable when it is called for a second time. If you include no seed or a fixed number for a seed, the program will act exactly the same, because the random number will be the same.