note 85099 modified in function.rand by thiago

From: Date: Thu, 14 Aug 2008 14:33:47 +0000
Subject: note 85099 modified in function.rand by thiago
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-143499@lists.php.net to get a copy of this message
Heres another function to generate Random Unique numbers. <?php class UniqueRand{ var $alreadyExists = array(); function uRand($min = NULL, $max = NULL){ $break='false'; while($break=='false'){ $rand=mt_rand($min,$max); if(array_search($rand,$this->alreadyExists)===false){ $this->alreadyExists[]=$rand; $break='stop'; }else{ echo " $rand already! "; print_r($this->alreadyExists); } } return $rand; } } $rand=new UniqueRand(); echo"<b> ". $rand->uRand(0,5)."</b>"; echo"<b> ". $rand->uRand(5,5)."</b>"; echo"<b> ". $rand->uRand(10,5)."</b>"; ?> --was-- Heres another function to generate Random Unique numbers. <? class UniqueRand{ var $alreadyExists = array(); function uRand($min = NULL, $max = NULL){ $break='false'; while($break=='false'){ $rand=mt_rand($min,$max); if(array_search($rand,$this->alreadyExists)===false){ $this->alreadyExists[]=$rand; $break='stop'; }else{ echo " $rand already! "; print_r($this->alreadyExists); } } return $rand; } } $rand=new UniqueRand(); echo"<b> ". $rand->uRand(0,5)."</b>"; echo"<b> ". $rand->uRand(5,5)."</b>"; echo"<b> ". $rand->uRand(10,5)."</b>"; ?> http://php.net/manual/en/function.rand.php

« previous php.notes (#143499) next »