PHP 8.4.15 Released!

Voting

: six plus two?
(Example: nine)

The Note You're Voting On

DiosDe
4 years ago
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;
}
?>

<< Back to user notes page

To Top