note 56872 added to function.rand
| From: | nick at nerdynick dot com | Date: | Sun, 18 Sep 2005 03:58:23 +0000 |
| Subject: | note 56872 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-95340@lists.php.net to get a copy of this message | ||
Here is a function I wrote to generate an array with a unigue set of numbers in each place. You can
enter the Min and the Max number to pick from and the number of array places to generate for.
random_loop([# of Array Places], [Min], [Max]);
<?php
$cols = random_loop(9, 0, 9);
function random_loop($num, $min, $max){
$rand[0] = mt_rand($min, $max);
for($i = 1; $i<$num; $i++){
do{
$check = true;
$rand[$i] = mt_rand($min, $max);
for($ii = 0; $ii < (count($rand)-1); $ii++){
if($rand[$i] == $rand[$ii] && $check){
$check = false;
break;
}
}
}while (!$check);
}
return $rand;
}
?>
Enjoy and use at your will.
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56872
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56872&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56872&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56872&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56872&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56872&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+56872&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56872&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56872&report=yes
Search -- http://master.php.net/manage/user-notes.php