Add shuffle.

This commit is contained in:
Drup 2016-01-16 16:35:33 +01:00
parent 964b5c61bd
commit 9ae652b004
3 changed files with 15 additions and 0 deletions

View file

@ -707,6 +707,11 @@ let shuffle_array a =
a.(k) <- tmp;
done
let shuffle seq =
let a = to_array seq in
shuffle_array a ;
of_array a
let shuffle_buffer n seq k =
let seq_front = take n seq in
let a = to_array seq_front in

View file

@ -498,6 +498,11 @@ val random_list : 'a list -> 'a t
(** Infinite sequence of random elements of the list. Basically the
same as {!random_array}. *)
val shuffle : 'a t -> 'a t
(** [shuffle seq] returns a perfect shuffle of [seq].
Uses O(length seq) memory and time. Eager.
@since NEXT_RELEASE *)
val shuffle_buffer : int -> 'a t -> 'a t
(** [shuffle_buffer n seq] returns a sequence of element of [seq] in random
order. The shuffling is *not* uniform. Uses O(n) memory.

View file

@ -446,6 +446,11 @@ val random_list : 'a list -> 'a t
(** Infinite sequence of random elements of the list. Basically the
same as {!random_array}. *)
val shuffle : 'a t -> 'a t
(** [shuffle seq] returns a perfect shuffle of [seq].
Uses O(length seq) memory and time. Eager.
@since NEXT_RELEASE *)
val shuffle_buffer : n:int -> 'a t -> 'a t
(** [shuffle_buffer n seq] returns a sequence of element of [seq] in random
order. The shuffling is not uniform. Uses O(n) memory.