i need to generate 3 number like 4,15,19 or 17,01,11 the numbers should not be same
can any one help me?
generate 3 random (not same) numbers within 1 to 20
Moderator: General Moderators
Re: generate 3 random (not same) numbers within 1 to 20
Use range to create an array of numbers one through twenty and array_rand to pick three random ones (they will be unique).
Re: generate 3 random (not same) numbers within 1 to 20
Code: Select all
function GetRandomNrs( $min=1, $max=20, $num=3 )
{
if ($num > $max-$min+1) $num = $max-$min+1;
$result = array();
for ($i=0; $i<$num; $i++)
{
do
{
$x = mt_rand($min,$max);
}
while (in_array($x,$result));
$result[] = $x;
}
return $result;
}For low numbers of possibilities (as in your case 1 to 20), the range function mentioned by tasairis is easier. If you want to pick 3 numbers between 1 and 100.000.000, the above function is faster.
Re: generate 3 random (not same) numbers within 1 to 20
thx a lot , it s working perfectly ... 