phparraysperformancerandomshuffle

Efficiently pick n random elements from PHP array (without shuffle)


I have the following code to pick $n elements from an array $array in PHP:

shuffle($array);
$result = array_splice($array, 0, $n);

Given a large array but only a few elements (for example 5 out of 10000), this is relatively slow, so I would like to optimize it such that not all elements have to be shuffled. The values must be unique.

I'm looking fo the most performant alternative. We can assume that $array has no duplicates and is 0-indexed.


Solution

  • $randomArray = [];
    while (count($randomArray) < 5) {
      $randomKey = mt_rand(0, count($array)-1);
      $randomArray[$randomKey] = $array[$randomKey];
    }
    

    This will provide exactly 5 elements with no duplicates and very quickly. The keys will be preserved.

    Note: You'd have to make sure $array had 5 or more elements or add some sort of check to prevent an endless loop.