Note Submitter: alishahnovin at hotmail dot com
----
Quick function that returns an array of unequal random numbers. Works well if you need to assign random values, but want them to all be unique.
<?php
function randiff($min, $max, $num) {
if ($min<$max && $max-$min+1 >= $num && $num>0) {
$random_nums = array();
$i=0;
while($i<$num) {
$rand_num = rand($min, $max);
if (!in_array($rand_num, $random_nums)) {
$random_nums[] = $rand_num;
$i++;
}
}
return $random_nums;
} else {
return false;
}
}
?>
So rather than:
<?php
$var1 = rand(0,10);
$var2 = rand(0,10);
?>
Which could potentially give you two of the same values, you can use:
<?php
$nums = randiff(0,10,2);
$var1 = $nums[0];
$var2 = $nums[1];
?>
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.