note 104039 added to function.srand
| From: | dev+php at alepe dot com | Date: | Thu, 19 May 2011 01:34:07 +0000 |
| Subject: | note 104039 added to function.srand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-179866@lists.php.net to get a copy of this message | ||
Be sure to "reset" srand() to a random seed once you use it (if you don't need the
seed after that).
For example:
<?php
function myRandomArray(&$array, $seed) {
srand($seed);
shuffle($array);
}
...
myRandomArray($array, 12345);
...
...
...
$y = rand(0,100); //<-- Will always return the same sequence as srand is still on effect.
?>
To prevent that, one way is to set the srand() back to "normal":
<?php
function myRandomArray(&$array, $seed) {
srand($seed);
shuffle($array);
srand(); //<-- this would do the trick
}
?>
This also applies for mt_srand().
----
Server IP: 69.147.83.197
Probable Submitter: 211.6.179.194
----
Manual Page -- http://www.php.net/manual/en/function.srand.php
Edit -- https://master.php.net/note/edit/104039
Del: integrated -- https://master.php.net/note/delete/104039/integrated
Del: useless -- https://master.php.net/note/delete/104039/useless
Del: bad code -- https://master.php.net/note/delete/104039/bad+code
Del: spam -- https://master.php.net/note/delete/104039/spam
Del: non-english -- https://master.php.net/note/delete/104039/non-english
Del: in docs -- https://master.php.net/note/delete/104039/in+docs
Del: other reasons-- https://master.php.net/note/delete/104039
Reject -- https://master.php.net/note/reject/104039
Search -- https://master.php.net/manage/user-notes.php