Source file array_permute.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
(** An internal-only module factored out due to a circular dependency between core_array
and core_list. Contains code for permuting an array. *)
open! Import
include Array0
let permute ?(random_state = Random.State.default) ?(pos = 0) ?len t =
let total_length = length t in
let len =
match len with
| Some l -> l
| None -> total_length - pos
in
Ordered_collection_common0.check_pos_len_exn ~pos ~len ~total_length;
let num_swaps = len - 1 in
for i = num_swaps downto 1 do
let this_i = pos + i in
let random_i = pos + Random.State.int random_state (i + 1) in
swap t this_i random_i
done
;;