note 8986 added to srand

From: Date: Tue, 10 Oct 2000 07:51:12 +0000
Subject: note 8986 added to srand
Groups: php.notes 
Request: Send a blank email to php-notes+get-6295@lists.php.net to get a copy of this message
It is REALLY essential to make sure that srand is called only once. This is a bit difficult if the call is hidden somewhere in third-party code you include. For example, I used a standard banner script that *seemed* to work well putting three random banners in one frame. But in the long run, the choice appeared somewhat biased - probably because srand was called once per banner, not once per run. It would be nice if the random number generator worked like in PERL: If You use the random function without having called srand ever before in a script, srand is invoked before (and automatically with a nice seed, hopefully). I suggest that one should do something like this: <pre> if (!$GLOBALS["IHaveCalledSrandBefore"]++) { srand((double) microtime() * 1000000); } </pre> (Depending on the situation, one might also work with a static variable instead) http://www.php.net/manual/function.srand.php

« previous php.notes (#6295) next »