mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-10 21:23:57 -05:00
rename Random.sample_without_{replacement,duplicates}
- deprecate sample_without_replacement - use `~cmp` as the named argument, more uniform close #130
This commit is contained in:
parent
f692fe5dd9
commit
6d1f0b9957
2 changed files with 17 additions and 4 deletions
|
|
@ -77,8 +77,8 @@ let replicate n g st =
|
||||||
in aux [] n
|
in aux [] n
|
||||||
|
|
||||||
(* Sample without replacement using rejection sampling. *)
|
(* Sample without replacement using rejection sampling. *)
|
||||||
let sample_without_replacement (type elt) ~compare k (rng:elt t) st=
|
let sample_without_duplicates (type elt) ~cmp k (rng:elt t) st=
|
||||||
let module S = Set.Make(struct type t=elt let compare = compare end) in
|
let module S = Set.Make(struct type t=elt let compare = cmp end) in
|
||||||
let rec aux s k =
|
let rec aux s k =
|
||||||
if k <= 0 then
|
if k <= 0 then
|
||||||
S.elements s
|
S.elements s
|
||||||
|
|
@ -89,9 +89,13 @@ let sample_without_replacement (type elt) ~compare k (rng:elt t) st=
|
||||||
else
|
else
|
||||||
aux (S.add x s) (k-1)
|
aux (S.add x s) (k-1)
|
||||||
in
|
in
|
||||||
if k<=0 then invalid_arg "sample_without_replacement";
|
if k<=0 then invalid_arg "sample_without_duplicates";
|
||||||
aux S.empty k
|
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 list_seq l st = List.map (fun f -> f st) l
|
||||||
|
|
||||||
let split i st =
|
let split i st =
|
||||||
|
|
@ -231,5 +235,5 @@ let uniformity_test ?(size_hint=10) k rng st =
|
||||||
|
|
||||||
(*$R
|
(*$R
|
||||||
let open Containers in
|
let open Containers in
|
||||||
ignore @@ List.random_choose [1;2;3] (Random.get_state())
|
ignore @@ List.random_choose [1;2;3] (Random.get_state())
|
||||||
*)
|
*)
|
||||||
|
|
|
||||||
|
|
@ -64,9 +64,18 @@ val sample_without_replacement:
|
||||||
(** [sample_without_replacement n g] makes a list of [n] elements which are all
|
(** [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
|
generated randomly using [g] with the added constraint that none of the generated
|
||||||
random values are equal.
|
random values are equal.
|
||||||
|
@deprecated use sample_without_duplicates instead
|
||||||
@raise Invalid_argument if [n <= 0].
|
@raise Invalid_argument if [n <= 0].
|
||||||
@since 0.15 *)
|
@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
|
val list_seq : 'a t list -> 'a list t
|
||||||
(** Build random lists from lists of random generators.
|
(** Build random lists from lists of random generators.
|
||||||
@since 0.4 *)
|
@since 0.4 *)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue