Module CCRandom
Random Generators
Documentation for the standard Random module
include module type of sig ... end
val init : int -> unitval full_init : int array -> unitval self_init : unit -> unitval bits : unit -> intval int : int -> intval int32 : Stdlib.Int32.t -> Stdlib.Int32.tval nativeint : Stdlib.Nativeint.t -> Stdlib.Nativeint.tval int64 : Stdlib.Int64.t -> Stdlib.Int64.tval float : float -> floatval bool : unit -> bool
type state= Stdlib.Random.State.ttype 'a t= state -> 'aRandom generator for values of type
'a.
type 'a random_gen= 'a t
val return : 'a -> 'a treturn xis the generator that always returnsx. Example:let random_int = return 4 (* fair dice roll *).
val (>|=) : 'a t -> ('a -> 'b) -> 'b tval delay : (unit -> 'a t) -> 'a tDelay evaluation. Useful for side-effectful generators that need some code to run for every call. Example:
let gensym = let r = ref 0 in fun () -> incr r; !r ;; delay (fun () -> let name = gensym() in small_int >>= fun i -> return (name,i) )- since
- 0.4
val choose_exn : 'a t list -> 'a tLike
choosebut without option.- raises Invalid_argument
if the list is empty.
val choose_return : 'a list -> 'a tChoose among the list.
- raises Invalid_argument
if the list is empty.
val replicate : int -> 'a t -> 'a list treplicate n gmakes a list ofnelements which are all generated randomly usingg.
val sample_without_duplicates : cmp:('a -> 'a -> int) -> int -> 'a t -> 'a list tsample_without_replacement n gmakes a list ofnelements which are all generated randomly usinggwith the added constraint that none of the generated random values are equal.- raises Invalid_argument
if
n <= 0.
- since
- 2.4
val pick_list : 'a list -> 'a tPick an element at random from the list.
- raises Pick_from_empty
if the list is empty.
- since
- 0.16
val pick_array : 'a array -> 'a tPick an element at random from the array.
- raises Pick_from_empty
if the array is empty.
- since
- 0.16
val small_int : int tA small int (100).
val int : int -> int tRandom int within the given range.
val int_range : int -> int -> int tInclusive range.
val small_float : float tA reasonably small float (100.0).
- since
- 0.6.1
val float : float -> float tRandom float within the given range.
- since
- 0.6.1
val float_range : float -> float -> float tInclusive range.
float_range a bassumesa < b.- since
- 0.6.1
val split : int -> (int * int) option tSplit a positive value
ninton1,n2wheren = n1 + n2.- returns
Noneif the value is too small.
val split_list : int -> len:int -> int list option tSplit a value
ninto a list of values whose sum isnand whose length islength. The list is never empty and does not contain0.- raises Invalid_argument
if
len <= 1.
- returns
Noneif the value is too small.
val retry : ?max:int -> 'a option t -> 'a option tretry gcallsguntil it returns some value, or until the maximum number of retries was reached. Ifgfails, then it counts for one iteration, and the generator retries.- parameter max:
maximum number of retries. Default
10.
val try_successively : 'a option t list -> 'a option ttry_successively ltries each generator ofl, one after the other. If some generator succeeds its result is returned, else the next generator is tried.
val (<?>) : 'a option t -> 'a option t -> 'a option ta <?> bis a choice operator. It first triesa, and returns its result if successful. Ifafails, thenbis returned.
val fix : ?sub1:('a t -> 'a t) list -> ?sub2:('a t -> 'a t -> 'a t) list -> ?subn:(int t * ('a list t -> 'a t)) list -> base:'a t -> int t -> 'a tRecursion combinators, for building recursive values. The integer generator is used to provide fuel. The
sub_generators should use their arguments only once!- parameter sub1
cases that recurse on one value.
- parameter sub2
cases that use the recursive gen twice.
- parameter subn
cases that use a list of recursive cases.
Applicative
Let operators on OCaml >= 4.08.0, nothing otherwise
- since
- 2.8