note 73797 deleted from function.mt-rand by cmb
| From: | cmb@php.net | Date: | Sun, 19 Jan 2020 08:37:16 +0000 |
| Subject: | note 73797 deleted from function.mt-rand by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213914@lists.php.net to get a copy of this message | ||
Note Submitter: Rene at mokiba dot de
----
Here is my Function to generate an Array with unique random Numbers between "$from" and
"$to".
function random_number_array($count, $from, $to){
for($i=$from;$i<$to;$i++){
$number_array[] = $i;
}
$random_number_array = array();
mt_srand ((double) microtime() * 1000000);
for($i=0;$i<$count;$i++){
$random_number = mt_rand(0,count($number_array)-1);
$key_to_insert_and_delete = array_search($random_number, $number_array);
$random_number_array[$i] = $number_array[$key_to_insert_and_delete];
array_splice($number_array, $key_to_insert_and_delete, 1);
}
// Array $random_number_array with $count random Numbers, between $from and $to
return $random_number_array;
}
I hope its helping you.
Greetings
Rene Andris