[PHP-NOTES] note 130617 added to function.shuffle

[email protected] ("[email protected]") Tue, 20 Jan 2026 17:11:32 +0000
Newsgroups php.notes
Message-ID <[email protected]>
@DiosDe your function wrong;

$random_index = random_int(0, $sizeof_array);
must be :
$random_index = random_int(0, $sizeof_array-1);

working version:
<?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 - 1);
        } while ( in_array($random_index, $skip_indexes) );

        $skip_indexes[] = $random_index;
        $result[] = $array[$random_index];
    }

    return $result;
}
?>
----
Server IP: 206.189.2.9
Probable Submitter: 85.108.185.0
----
Manual Page -- https://php.net/manual/en/function.shuffle.php
Edit        -- https://main.php.net/note/edit/130617
Del: integrated  -- https://main.php.net/note/delete/130617/integrated
Del: useless     -- https://main.php.net/note/delete/130617/useless
Del: bad code    -- https://main.php.net/note/delete/130617/bad+code
Del: spam        -- https://main.php.net/note/delete/130617/spam
Del: non-english -- https://main.php.net/note/delete/130617/non-english
Del: in docs     -- https://main.php.net/note/delete/130617/in+docs
Del: other reasons-- https://main.php.net/note/delete/130617
Reject      -- https://main.php.net/note/reject/130617
Search      -- https://main.php.net/manage/user-notes.php