package containers
Install
Dune Dependency
Authors
Maintainers
Sources
md5=14787fb6878a94dd728a0ef7e368ab89
sha512=9debbd79542fbe24e6b0ec5e0fb74077566663fa53b868aa381962653d65543a86606ed6703a75cf3e14962b66068747b237a88bb1eea15b6062665e294795ac
doc/containers/CCRandom/index.html
Module CCRandom
Source
Random Generators
include module type of struct include Random end
Basic functions
Initialize the generator, using the argument as a seed. The same seed will always yield the same sequence of numbers.
Same as Random.init
but takes more data as seed.
Initialize the generator with a random seed chosen in a system-dependent way. If /dev/urandom
is available on the host machine, it is used to provide a highly random initial seed. Otherwise, a less random seed is computed from system parameters (current time, process IDs).
Return 30 random bits in a nonnegative integer.
Random.full_int bound
returns a random integer between 0 (inclusive) and bound
(exclusive). bound
may be any positive integer.
If bound
is less than 230, Random.full_int bound
is equal to Random.int
bound
. If bound
is greater than 230 (on 64-bit systems or non-standard environments, such as JavaScript), Random.full_int
returns a value, where Random.int
raises Invalid_argument
.
Random.int32 bound
returns a random integer between 0 (inclusive) and bound
(exclusive). bound
must be greater than 0.
Random.nativeint bound
returns a random integer between 0 (inclusive) and bound
(exclusive). bound
must be greater than 0.
Random.int64 bound
returns a random integer between 0 (inclusive) and bound
(exclusive). bound
must be greater than 0.
Random.bool ()
returns true
or false
with probability 0.5 each.
Random.bits32 ()
returns 32 random bits as an integer between Int32.min_int
and Int32.max_int
.
Random.bits64 ()
returns 64 random bits as an integer between Int64.min_int
and Int64.max_int
.
Random.nativebits ()
returns 32 or 64 random bits (depending on the bit width of the platform) as an integer between Nativeint.min_int
and Nativeint.max_int
.
Advanced functions
The functions from module State
manipulate the current state of the random generator explicitly. This allows using one or several deterministic PRNGs, even in a multi-threaded program, without interference from other parts of the program.
Return the current state of the generator used by the basic functions.
return x
is the generator that always returns x
. Example: let random_int = return 4 (* fair dice roll *)
.
Delay 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)
)
replicate n g
makes a list of n
elements which are all generated randomly using g
.
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.
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 contain 0
.
retry g
calls g
until it returns some value, or until the maximum number of retries was reached. If g
fails, then it counts for one iteration, and the generator retries.
try_successively l
tries each generator of l
, one after the other. If some generator succeeds its result is returned, else the next generator is tried.
a <?> b
is a choice operator. It first tries a
, and returns its result if successful. If a
fails, then b
is 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 t
Recursion combinators, for building recursive values. The integer generator is used to provide fuel. The sub_
generators should use their arguments only once!
Applicative
Let operators on OCaml >= 4.08.0, nothing otherwise