diff --git a/sequence.ml b/sequence.ml index b68b474..b1d9ad4 100644 --- a/sequence.ml +++ b/sequence.ml @@ -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 diff --git a/sequence.mli b/sequence.mli index 1393490..83f4753 100644 --- a/sequence.mli +++ b/sequence.mli @@ -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. diff --git a/sequenceLabels.mli b/sequenceLabels.mli index 8ab7a59..b52ac74 100644 --- a/sequenceLabels.mli +++ b/sequenceLabels.mli @@ -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.