It would be useful to include a function within the Random module to shuffle an array. I think this is a fairly fundamental operation and therefore worthy of inclusion in the OCaml standard library rather than having to download/install an extension module to do it.

It's also a function that's significantly error-prone to implement, so having a correct version in the standard library would be a handy so that individual programmers don't have to code this function from scratch in their applications.

Additional Information

(** [shuffle a] shuffles an array, giving a uniform random distribution *)
let shuffle a =
for i = pred (Array.length a) downto 1 do
let j = Random.int (succ i) in
if i <> j (* faster to omit this test with arrays of about 100000 elements or more *) then (
let tmp = Array.unsafe_get a i in
Array.unsafe_set a i (Array.unsafe_get a j);
Array.unsafe_set a j tmp
)
done