I understand that time(0) is commonly using for seeding random number generators and that it only becomes a problem when the program is being run more than once per second. I'm wondering what are some better seeds to consider when generating random numbers. I read about GetTickCount, timeGetTime, and QueryPerformanceCounter on Windows. Will these suffice for almost all operations or are there even better seeding options?
Here is a quick code example using the boost library:
#include <iostream>
#include <boost/random.hpp>
using namespace std;
using namespace boost;
int main()
{
mt19937 randGen(42);
uniform_int<> range(0,100);
variate_generator<mt19937&, uniform_int<> > GetRand(randGen, range);
for (int i = 0; i < 30; ++i)
cout << GetRand() << endl;
}
Too long for a comment but interesting story about 32bit seeds in the early days of online poker
http://www.ibm.com/developerworks/library/s-playing/
The method with random number generators is to only seed it once so your example of an online game is not a problem as, potentially, the same rng will be used for each value which would have been seeded when the program was first started (perhaps several years ago).
Similarly in your own code try to seed the rng once and then use the same instance where ever required rather than creating a new rng with a new seed all over the place.
Using tickCout() or anything with a high frequency is a bad idea. This is becuase the couter cycles back to zero very quickly thus gives the posability of having the same seed.
You could try and get some random value from nature.
Lightining strikes world wide in the last second (appatently that is online)? (You may need to do research to see if that is variable though).
You will need an alternative/secondary source of entropy. Depending on how much entropy you want to use, you can calculate a hash of any of the following inputs and use it as a seed for your final generator:
CryptGenRandom
to get a buffer of cryptographically random bytes/dev/random
On unix systems, you could take a few bytes from /dev/random as a seed for your RNG. /dev/random is supposed to be very good random, using the different entropy sources available on a PC. Of course, this is completely implementation-dependent.
One case in which this could be useful is for cryptographic applications, since time(0) is relatively easy to guess.