note 104039 modified in function.srand by danbrown

From: Date: Thu, 19 May 2011 13:54:12 +0000
Subject: note 104039 modified in function.srand by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-179881@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(). --was-- 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(). http://php.net/manual/en/function.srand.php

« previous php.notes (#179881) next »