I have the following code to pick $n
elements from an array $array
in PHP:
shuffle($array);
$result = array_splice($array, 0, $n);
This will only show benifits for small n
compared to an array shuffle, but you could
r
n
times, each time decreasing the limit by 1
Pseudocode
arr = []
used = []
for i = 0..n-1:
r = rand 0..len-i
d = 0
for j = 0..used.length-1:
if r >= used[j]:
d += 1
arr.append($array[r + d])
used.append(r)
return arr