note 126260 added to function.shuffle
| From: | DiosDe at main dot php dot net | Date: | Wed, 21 Jul 2021 17:03:50 +0000 |
| Subject: | note 126260 added to function.shuffle | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-216631@lists.php.net to get a copy of this message | ||
implementation of shuffle() using random_int()
<?php
function random_int_shuffle ( array $array ): array
{
$array = array_values($array);
$result = [];
$skip_indexes = [];
$sizeof_array = count($array);
for ( $i = 0; $i < $sizeof_array; $i++ )
{
do
{
$random_index = random_int(0, $sizeof_array);
} while ( in_array($random_index, $skip_indexes) );
$skip_indexes[] = $random_index;
$result[] = $array[$random_index];
}
return $result;
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 85.192.189.63)
----
Manual Page -- https://php.net/manual/en/function.shuffle.php
Edit -- https://main.php.net/note/edit/126260
Del: integrated -- https://main.php.net/note/delete/126260/integrated
Del: useless -- https://main.php.net/note/delete/126260/useless
Del: bad code -- https://main.php.net/note/delete/126260/bad+code
Del: spam -- https://main.php.net/note/delete/126260/spam
Del: non-english -- https://main.php.net/note/delete/126260/non-english
Del: in docs -- https://main.php.net/note/delete/126260/in+docs
Del: other reasons-- https://main.php.net/note/delete/126260
Reject -- https://main.php.net/note/reject/126260
Search -- https://main.php.net/manage/user-notes.php