Re: Random Number Excluding Specified Value in Array
- From: "Rik" <luiheidsgoeroe@xxxxxxxxxxx>
- Date: Mon, 17 Jul 2006 22:10:09 +0200
AP wrote:
Hello
Any suggestions on the most efficient way to generate a random number
from a range that will not return values specified in an array?
Euhm, I am not really sure what you mean. Like this?
For integers:
function
rand_number_without_certain_values_for_some_reason($start,$end,$not){
$i = 0;
do{
$i++;
if($i > 5000) return false;
$int = mt_renc($start,$end);
} while (is_array($not) && in_array($int,$not));
return $int;
}
Had to build in safety (the $i) to avoid endless looping when using
something like (1,4,array(range(1,4)))
Grtz,
--
Rik Wasmus
.
- References:
- Prev by Date: Re: Login / Password
- Next by Date: Re: Random Number Excluding Specified Value in Array
- Previous by thread: Re: Random Number Excluding Specified Value in Array
- Next by thread: PHP 5 DomDocument
- Index(es):
Relevant Pages
|