diff --git a/src/core/CCRandom.ml b/src/core/CCRandom.ml index ded871ff..a6babfb9 100644 --- a/src/core/CCRandom.ml +++ b/src/core/CCRandom.ml @@ -77,8 +77,8 @@ let replicate n g st = in aux [] n (* Sample without replacement using rejection sampling. *) -let sample_without_replacement (type elt) ~compare k (rng:elt t) st= - let module S = Set.Make(struct type t=elt let compare = compare end) in +let sample_without_duplicates (type elt) ~cmp k (rng:elt t) st= + let module S = Set.Make(struct type t=elt let compare = cmp end) in let rec aux s k = if k <= 0 then S.elements s @@ -89,9 +89,13 @@ let sample_without_replacement (type elt) ~compare k (rng:elt t) st= else aux (S.add x s) (k-1) in - if k<=0 then invalid_arg "sample_without_replacement"; + if k<=0 then invalid_arg "sample_without_duplicates"; aux S.empty k +(* deprecated *) +let sample_without_replacement ~compare k rng = + sample_without_duplicates ~cmp:compare k rng + let list_seq l st = List.map (fun f -> f st) l let split i st = @@ -231,5 +235,5 @@ let uniformity_test ?(size_hint=10) k rng st = (*$R let open Containers in - ignore @@ List.random_choose [1;2;3] (Random.get_state()) + ignore @@ List.random_choose [1;2;3] (Random.get_state()) *) diff --git a/src/core/CCRandom.mli b/src/core/CCRandom.mli index b2cf3442..c52852d5 100644 --- a/src/core/CCRandom.mli +++ b/src/core/CCRandom.mli @@ -64,9 +64,18 @@ val sample_without_replacement: (** [sample_without_replacement n g] makes a list of [n] elements which are all generated randomly using [g] with the added constraint that none of the generated random values are equal. + @deprecated use sample_without_duplicates instead @raise Invalid_argument if [n <= 0]. @since 0.15 *) +val sample_without_duplicates: + cmp:('a -> 'a -> int) -> int -> 'a t -> 'a list t +(** [sample_without_replacement n g] makes a list of [n] elements which are all + generated randomly using [g] with the added constraint that none of the generated + random values are equal. + @raise Invalid_argument if [n <= 0]. + @since NEXT_RELEASE *) + val list_seq : 'a t list -> 'a list t (** Build random lists from lists of random generators. @since 0.4 *)