mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2026-01-29 04:14:51 -05:00
also fail in CCRandom.sample_without_replacement if n<=0
This commit is contained in:
parent
203d870357
commit
b1b8bc1096
2 changed files with 6 additions and 3 deletions
|
|
@ -85,7 +85,9 @@ let sample_without_replacement (type elt) ?(compare=compare) k (rng:elt t) st=
|
||||||
if S.mem x s then
|
if S.mem x s then
|
||||||
aux s k
|
aux s k
|
||||||
else
|
else
|
||||||
aux (S.add x s) (k-1) in
|
aux (S.add x s) (k-1)
|
||||||
|
in
|
||||||
|
if k<=0 then invalid_arg "sample_without_replacement";
|
||||||
aux S.empty k
|
aux S.empty k
|
||||||
|
|
||||||
let list_seq l st = List.map (fun f -> f st) l
|
let list_seq l st = List.map (fun f -> f st) l
|
||||||
|
|
@ -112,7 +114,7 @@ let _diff_list ~last l =
|
||||||
If we define, y_k = x_{k+1} - x_{k} for k in 0..(len-1), then by construction
|
If we define, y_k = x_{k+1} - x_{k} for k in 0..(len-1), then by construction
|
||||||
∑_k y_k = ∑_k (x_{k+1} - x_k ) = x_{len} - x_0 = i. *)
|
∑_k y_k = ∑_k (x_{k+1} - x_k ) = x_{len} - x_0 = i. *)
|
||||||
let split_list i ~len st =
|
let split_list i ~len st =
|
||||||
if len=0 then invalid_arg "Random.split_list";
|
if len <= 0 then invalid_arg "Random.split_list";
|
||||||
if i >= len then
|
if i >= len then
|
||||||
let xs = sample_without_replacement (len-1) (int_range 1 (i-1)) st in
|
let xs = sample_without_replacement (len-1) (int_range 1 (i-1)) st in
|
||||||
_diff_list ( 0::xs ) ~last:i
|
_diff_list ( 0::xs ) ~last:i
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,7 @@ 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
|
||||||
|
@raise Invalid_argument if [n <= 0]
|
||||||
@since 0.15 *)
|
@since 0.15 *)
|
||||||
|
|
||||||
val list_seq : 'a t list -> 'a list t
|
val list_seq : 'a t list -> 'a list t
|
||||||
|
|
@ -104,7 +105,7 @@ val split_list : int -> len:int -> int list option t
|
||||||
(** Split a value [n] into a list of values whose sum is [n]
|
(** Split a value [n] into a list of values whose sum is [n]
|
||||||
and whose length is [length]. The list is never empty and does not
|
and whose length is [length]. The list is never empty and does not
|
||||||
contain [0].
|
contain [0].
|
||||||
@raise Invalid_argument if [len=0]
|
@raise Invalid_argument if [len <= 0]
|
||||||
@return [None] if the value is too small *)
|
@return [None] if the value is too small *)
|
||||||
|
|
||||||
val retry : ?max:int -> 'a option t -> 'a option t
|
val retry : ?max:int -> 'a option t -> 'a option t
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue