I was trying to find the right answer to the question below and here are the 2 answers I come up with:

srand(time(NULL));
or
srand(clock());

If we want the random number library function to produce a different sequence of values each time the program is run which of the following statements should be used?

Recommended Answers

All 3 Replies

srand((unsigned)time(NULL)); is what most people use if I am correct, I know for sure I have always used it and always will, unless someone puts forward a good argument not to use it.

Just remember you only need to call srand() once in your application.

Chris

clock() will get the number of "ticks" since your program started. If this statement is executed at the same point in the program several times (likely), you will see repetition. time() is the better choice, if not for randomness, then just for commonality.

srand((unsigned int)time(NULL));

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.