I have just noticed that I am a bad boy and call srand() and mt_srand() several times during a script's execution. I am thinking of making makeSeed() and seedGenerators() functions that are only called once per script execution.

Maybe one day I will add a argument dealing with the "goodness" of the seed, but for now I'll just stick with the trusty formula for the seed:

(double)microtime()*1000003

My question is if it is even worth calling srand() and mt_srand() for that seed, since newer versions of PHP automatically seed the generator for you if you call rand() or mt_rand() without first calling srand() or mt_srand()? How good are the automagically-generated seeds that PHP uses?

I'm trying to keep performance in mind.

    furthermore, is there ever a reason to use rand() over mt_rand()?

      rand() will probably show slightly better performance; but the platform variability (and weak implementations on some platforms: the Windows build for example) make mt_rand() the more reliable choice - there's a reason why rand() is typically faster, and it's not a good one!

      As for setting the seed: PHP's seed generator is good enough: I've mentioned in a previous post how PHP uses the microtime() and salts that from an internal RNG (also accessable in userspace via lcg_value()) to generates its seed (in other words, your seed generation is significantly weaker). You're free to have a poke around in the PHP source code and see just how it works. The only reason why you'd want to set the seed yourself is because you want to obtain the same reproducible sequences every time (and if you want to do that, you really do want to use mt_rand()).

        mt_rand is actually 4 times quicker than rand accordingt' manual, and displays better random characteristics than rand, so leave rand alone to rot.

          Write a Reply...