ocaml-containers/1.4/CCRandom.html
2017-12-24 16:48:20 +01:00

255 lines
No EOL
18 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="Start" href="index.html">
<link rel="previous" href="CCOrd.html">
<link rel="next" href="CCString.html">
<link rel="Up" href="index.html">
<link title="Index of types" rel=Appendix href="index_types.html">
<link title="Index of exceptions" rel=Appendix href="index_exceptions.html">
<link title="Index of values" rel=Appendix href="index_values.html">
<link title="Index of class methods" rel=Appendix href="index_methods.html">
<link title="Index of class types" rel=Appendix href="index_class_types.html">
<link title="Index of modules" rel=Appendix href="index_modules.html">
<link title="Index of module types" rel=Appendix href="index_module_types.html">
<link title="CCVector" rel="Chapter" href="CCVector.html">
<link title="CCHeap" rel="Chapter" href="CCHeap.html">
<link title="CCList" rel="Chapter" href="CCList.html">
<link title="CCOpt" rel="Chapter" href="CCOpt.html">
<link title="CCPair" rel="Chapter" href="CCPair.html">
<link title="CCFun" rel="Chapter" href="CCFun.html">
<link title="CCHash" rel="Chapter" href="CCHash.html">
<link title="CCInt" rel="Chapter" href="CCInt.html">
<link title="CCBool" rel="Chapter" href="CCBool.html">
<link title="CCFloat" rel="Chapter" href="CCFloat.html">
<link title="CCArray" rel="Chapter" href="CCArray.html">
<link title="CCRef" rel="Chapter" href="CCRef.html">
<link title="CCSet" rel="Chapter" href="CCSet.html">
<link title="CCOrd" rel="Chapter" href="CCOrd.html">
<link title="CCRandom" rel="Chapter" href="CCRandom.html">
<link title="CCString" rel="Chapter" href="CCString.html">
<link title="CCHashtbl" rel="Chapter" href="CCHashtbl.html">
<link title="CCMap" rel="Chapter" href="CCMap.html">
<link title="CCFormat" rel="Chapter" href="CCFormat.html">
<link title="CCIO" rel="Chapter" href="CCIO.html">
<link title="CCInt64" rel="Chapter" href="CCInt64.html">
<link title="CCChar" rel="Chapter" href="CCChar.html">
<link title="CCResult" rel="Chapter" href="CCResult.html">
<link title="CCParse" rel="Chapter" href="CCParse.html">
<link title="CCArray_slice" rel="Chapter" href="CCArray_slice.html">
<link title="CCListLabels" rel="Chapter" href="CCListLabels.html">
<link title="CCArrayLabels" rel="Chapter" href="CCArrayLabels.html">
<link title="CCEqual" rel="Chapter" href="CCEqual.html">
<link title="Containers" rel="Chapter" href="Containers.html">
<link title="CCKTree" rel="Chapter" href="CCKTree.html">
<link title="CCKList" rel="Chapter" href="CCKList.html">
<link title="CCLazy_list" rel="Chapter" href="CCLazy_list.html">
<link title="CCMultiMap" rel="Chapter" href="CCMultiMap.html">
<link title="CCMultiSet" rel="Chapter" href="CCMultiSet.html">
<link title="CCTrie" rel="Chapter" href="CCTrie.html">
<link title="CCFlatHashtbl" rel="Chapter" href="CCFlatHashtbl.html">
<link title="CCCache" rel="Chapter" href="CCCache.html">
<link title="CCPersistentHashtbl" rel="Chapter" href="CCPersistentHashtbl.html">
<link title="CCDeque" rel="Chapter" href="CCDeque.html">
<link title="CCFQueue" rel="Chapter" href="CCFQueue.html">
<link title="CCBV" rel="Chapter" href="CCBV.html">
<link title="CCMixtbl" rel="Chapter" href="CCMixtbl.html">
<link title="CCMixmap" rel="Chapter" href="CCMixmap.html">
<link title="CCRingBuffer" rel="Chapter" href="CCRingBuffer.html">
<link title="CCIntMap" rel="Chapter" href="CCIntMap.html">
<link title="CCPersistentArray" rel="Chapter" href="CCPersistentArray.html">
<link title="CCMixset" rel="Chapter" href="CCMixset.html">
<link title="CCGraph" rel="Chapter" href="CCGraph.html">
<link title="CCHashSet" rel="Chapter" href="CCHashSet.html">
<link title="CCBitField" rel="Chapter" href="CCBitField.html">
<link title="CCHashTrie" rel="Chapter" href="CCHashTrie.html">
<link title="CCWBTree" rel="Chapter" href="CCWBTree.html">
<link title="CCRAL" rel="Chapter" href="CCRAL.html">
<link title="CCSimple_queue" rel="Chapter" href="CCSimple_queue.html">
<link title="CCImmutArray" rel="Chapter" href="CCImmutArray.html">
<link title="CCHet" rel="Chapter" href="CCHet.html">
<link title="CCZipper" rel="Chapter" href="CCZipper.html">
<link title="CCPool" rel="Chapter" href="CCPool.html">
<link title="CCLock" rel="Chapter" href="CCLock.html">
<link title="CCSemaphore" rel="Chapter" href="CCSemaphore.html">
<link title="CCThread" rel="Chapter" href="CCThread.html">
<link title="CCBlockingQueue" rel="Chapter" href="CCBlockingQueue.html">
<link title="CCTimer" rel="Chapter" href="CCTimer.html">
<link title="CCUnix" rel="Chapter" href="CCUnix.html">
<link title="CCSexp" rel="Chapter" href="CCSexp.html">
<link title="CCSexp_lex" rel="Chapter" href="CCSexp_lex.html"><link title="Applicative" rel="Section" href="#6_Applicative">
<link title="Run a generator" rel="Section" href="#6_Runagenerator">
<title>Containers doc : CCRandom</title>
</head>
<body>
<div class="navbar"><a class="pre" href="CCOrd.html" title="CCOrd">Previous</a>
&nbsp;<a class="up" href="index.html" title="Index">Up</a>
&nbsp;<a class="post" href="CCString.html" title="CCString">Next</a>
</div>
<h1>Module <a href="type_CCRandom.html">CCRandom</a></h1>
<pre><span class="keyword">module</span> CCRandom: <code class="code"><span class="keyword">sig</span></code> <a href="CCRandom.html">..</a> <code class="code"><span class="keyword">end</span></code></pre><div class="info module top">
<h1 id="1_RandomGenerators">Random Generators</h1><br>
</div>
<hr width="100%">
<pre><span class="keyword">include</span> Random</pre>
<pre><span id="TYPEstate"><span class="keyword">type</span> <code class="type"></code>state</span> = <code class="type">Random.State.t</code> </pre>
<pre><span id="TYPEt"><span class="keyword">type</span> <code class="type">'a</code> t</span> = <code class="type"><a href="CCRandom.html#TYPEstate">state</a> -> 'a</code> </pre>
<div class="info ">
Random generator for values of type <code class="code"><span class="keywordsign">'</span>a</code><br>
</div>
<pre><span id="TYPErandom_gen"><span class="keyword">type</span> <code class="type">'a</code> random_gen</span> = <code class="type">'a <a href="CCRandom.html#TYPEt">t</a></code> </pre>
<pre><span id="VALreturn"><span class="keyword">val</span> return</span> : <code class="type">'a -> 'a <a href="CCRandom.html#TYPEt">t</a></code></pre><div class="info ">
<code class="code">return&nbsp;x</code> is the generator that always returns <code class="code">x</code>.
Example: <code class="code"><span class="keyword">let</span>&nbsp;random_int&nbsp;=&nbsp;return&nbsp;4&nbsp;<span class="comment">(*&nbsp;fair&nbsp;dice&nbsp;roll&nbsp;*)</span></code><br>
</div>
<pre><span id="VALflat_map"><span class="keyword">val</span> flat_map</span> : <code class="type">('a -> 'b <a href="CCRandom.html#TYPEt">t</a>) -> 'a <a href="CCRandom.html#TYPEt">t</a> -> 'b <a href="CCRandom.html#TYPEt">t</a></code></pre>
<pre><span id="VAL(>>=)"><span class="keyword">val</span> (&gt;&gt;=)</span> : <code class="type">'a <a href="CCRandom.html#TYPEt">t</a> -> ('a -> 'b <a href="CCRandom.html#TYPEt">t</a>) -> 'b <a href="CCRandom.html#TYPEt">t</a></code></pre>
<pre><span id="VALmap"><span class="keyword">val</span> map</span> : <code class="type">('a -> 'b) -> 'a <a href="CCRandom.html#TYPEt">t</a> -> 'b <a href="CCRandom.html#TYPEt">t</a></code></pre>
<pre><span id="VAL(>|=)"><span class="keyword">val</span> (&gt;|=)</span> : <code class="type">'a <a href="CCRandom.html#TYPEt">t</a> -> ('a -> 'b) -> 'b <a href="CCRandom.html#TYPEt">t</a></code></pre>
<pre><span id="VALdelay"><span class="keyword">val</span> delay</span> : <code class="type">(unit -> 'a <a href="CCRandom.html#TYPEt">t</a>) -> 'a <a href="CCRandom.html#TYPEt">t</a></code></pre><div class="info ">
Delay evaluation. Useful for side-effectful generators that
need some code to run for every call.
Example:
<pre class="codepre"><code class="code"> <span class="keyword">let</span> gensym = <span class="keyword">let</span> r = ref 0 <span class="keyword">in</span> <span class="keyword">fun</span> () <span class="keywordsign">-&gt;</span> incr r; !r ;;
delay (<span class="keyword">fun</span> () <span class="keywordsign">-&gt;</span>
<span class="keyword">let</span> name = gensym() <span class="keyword">in</span>
small_int &gt;&gt;= <span class="keyword">fun</span> i <span class="keywordsign">-&gt;</span> return (name,i)
)
</code></pre><br>
<b>Since</b> 0.4<br>
</div>
<pre><span id="VALchoose"><span class="keyword">val</span> choose</span> : <code class="type">'a <a href="CCRandom.html#TYPEt">t</a> list -> 'a option <a href="CCRandom.html#TYPEt">t</a></code></pre><div class="info ">
Choose a generator within the list.<br>
</div>
<pre><span id="VALchoose_exn"><span class="keyword">val</span> choose_exn</span> : <code class="type">'a <a href="CCRandom.html#TYPEt">t</a> list -> 'a <a href="CCRandom.html#TYPEt">t</a></code></pre><div class="info ">
Same as <a href="CCRandom.html#VALchoose"><code class="code"><span class="constructor">CCRandom</span>.choose</code></a> but without option.<br>
<b>Raises</b> <code>Invalid_argument</code> if the list is empty<br>
</div>
<pre><span id="VALchoose_array"><span class="keyword">val</span> choose_array</span> : <code class="type">'a <a href="CCRandom.html#TYPEt">t</a> array -> 'a option <a href="CCRandom.html#TYPEt">t</a></code></pre>
<pre><span id="VALchoose_return"><span class="keyword">val</span> choose_return</span> : <code class="type">'a list -> 'a <a href="CCRandom.html#TYPEt">t</a></code></pre><div class="info ">
Choose among the list<br>
<b>Raises</b> <code>Invalid_argument</code> if the list is empty<br>
</div>
<pre><span id="VALreplicate"><span class="keyword">val</span> replicate</span> : <code class="type">int -> 'a <a href="CCRandom.html#TYPEt">t</a> -> 'a list <a href="CCRandom.html#TYPEt">t</a></code></pre><div class="info ">
<code class="code">replicate&nbsp;n&nbsp;g</code> makes a list of <code class="code">n</code> elements which are all generated
randomly using <code class="code">g</code><br>
</div>
<pre><span id="VALsample_without_replacement"><span class="keyword">val</span> sample_without_replacement</span> : <code class="type">?compare:('a -> 'a -> int) -> int -> 'a <a href="CCRandom.html#TYPEt">t</a> -> 'a list <a href="CCRandom.html#TYPEt">t</a></code></pre><div class="info ">
<code class="code">sample_without_replacement&nbsp;n&nbsp;g</code> makes a list of <code class="code">n</code> elements which are all
generated randomly using <code class="code">g</code> with the added constraint that none of the generated
random values are equal<br>
<b>Since</b> 0.15<br>
<b>Raises</b> <code>Invalid_argument</code> if <code class="code">n&nbsp;&lt;=&nbsp;0</code><br>
</div>
<pre><span id="VALlist_seq"><span class="keyword">val</span> list_seq</span> : <code class="type">'a <a href="CCRandom.html#TYPEt">t</a> list -> 'a list <a href="CCRandom.html#TYPEt">t</a></code></pre><div class="info ">
Build random lists from lists of random generators<br>
<b>Since</b> 0.4<br>
</div>
<pre><span id="EXCEPTIONPick_from_empty"><span class="keyword">exception</span> Pick_from_empty</span></pre>
<div class="info ">
<b>Since</b> 0.16<br>
</div>
<pre><span id="VALpick_list"><span class="keyword">val</span> pick_list</span> : <code class="type">'a list -> 'a <a href="CCRandom.html#TYPEt">t</a></code></pre><div class="info ">
Pick an element at random from the list<br>
<b>Since</b> 0.16<br>
<b>Raises</b> <code>Pick_from_empty</code> if the list is empty<br>
</div>
<pre><span id="VALpick_array"><span class="keyword">val</span> pick_array</span> : <code class="type">'a array -> 'a <a href="CCRandom.html#TYPEt">t</a></code></pre><div class="info ">
Pick an element at random from the array<br>
<b>Since</b> 0.16<br>
<b>Raises</b> <code>Pick_from_empty</code> if the array is empty<br>
</div>
<pre><span id="VALsmall_int"><span class="keyword">val</span> small_int</span> : <code class="type">int <a href="CCRandom.html#TYPEt">t</a></code></pre>
<pre><span id="VALint"><span class="keyword">val</span> int</span> : <code class="type">int -> int <a href="CCRandom.html#TYPEt">t</a></code></pre>
<pre><span id="VALint_range"><span class="keyword">val</span> int_range</span> : <code class="type">int -> int -> int <a href="CCRandom.html#TYPEt">t</a></code></pre><div class="info ">
Inclusive range<br>
</div>
<pre><span id="VALsmall_float"><span class="keyword">val</span> small_float</span> : <code class="type">float <a href="CCRandom.html#TYPEt">t</a></code></pre><div class="info ">
A reasonably small float.<br>
<b>Since</b> 0.6.1<br>
</div>
<pre><span id="VALfloat"><span class="keyword">val</span> float</span> : <code class="type">float -> float <a href="CCRandom.html#TYPEt">t</a></code></pre><div class="info ">
Random float within the given range<br>
<b>Since</b> 0.6.1<br>
</div>
<pre><span id="VALfloat_range"><span class="keyword">val</span> float_range</span> : <code class="type">float -> float -> float <a href="CCRandom.html#TYPEt">t</a></code></pre><div class="info ">
Inclusive range. <code class="code">float_range&nbsp;a&nbsp;b</code> assumes <code class="code">a&nbsp;&lt;&nbsp;b</code>.<br>
<b>Since</b> 0.6.1<br>
</div>
<pre><span id="VALsplit"><span class="keyword">val</span> split</span> : <code class="type">int -> (int * int) option <a href="CCRandom.html#TYPEt">t</a></code></pre><div class="info ">
Split a positive value <code class="code">n</code> into <code class="code">n1,n2</code> where <code class="code">n&nbsp;=&nbsp;n1&nbsp;+&nbsp;n2</code>.<br>
<b>Returns</b> <code class="code"><span class="constructor">None</span></code> if the value is too small<br>
</div>
<pre><span id="VALsplit_list"><span class="keyword">val</span> split_list</span> : <code class="type">int -> len:int -> int list option <a href="CCRandom.html#TYPEt">t</a></code></pre><div class="info ">
Split a value <code class="code">n</code> into a list of values whose sum is <code class="code">n</code>
and whose length is <code class="code">length</code>. The list is never empty and does not
contain <code class="code">0</code>.<br>
<b>Raises</b> <code>Invalid_argument</code> if <code class="code">len&nbsp;&lt;=&nbsp;1</code><br>
<b>Returns</b> <code class="code"><span class="constructor">None</span></code> if the value is too small<br>
</div>
<pre><span id="VALretry"><span class="keyword">val</span> retry</span> : <code class="type">?max:int -> 'a option <a href="CCRandom.html#TYPEt">t</a> -> 'a option <a href="CCRandom.html#TYPEt">t</a></code></pre><div class="info ">
<code class="code">retry&nbsp;g</code> calls <code class="code">g</code> until it returns some value, or until the maximum
number of retries was reached. If <code class="code">g</code> fails,
then it counts for one iteration, and the generator retries.<br>
</div>
<pre><span id="VALtry_successively"><span class="keyword">val</span> try_successively</span> : <code class="type">'a option <a href="CCRandom.html#TYPEt">t</a> list -> 'a option <a href="CCRandom.html#TYPEt">t</a></code></pre><div class="info ">
<code class="code">try_successively&nbsp;l</code> tries each generator of <code class="code">l</code>, one after the other.
If some generator succeeds its result is returned, else the
next generator is tried<br>
</div>
<pre><span id="VAL(<?>)"><span class="keyword">val</span> (&lt;?&gt;)</span> : <code class="type">'a option <a href="CCRandom.html#TYPEt">t</a> -> 'a option <a href="CCRandom.html#TYPEt">t</a> -> 'a option <a href="CCRandom.html#TYPEt">t</a></code></pre><div class="info ">
<code class="code">a&nbsp;&lt;?&gt;&nbsp;b</code> is a choice operator. It first tries <code class="code">a</code>, and returns its
result if successful. If <code class="code">a</code> fails, then <code class="code">b</code> is returned.<br>
</div>
<pre><span id="VALfix"><span class="keyword">val</span> fix</span> : <code class="type">?sub1:('a <a href="CCRandom.html#TYPEt">t</a> -> 'a <a href="CCRandom.html#TYPEt">t</a>) list -><br> ?sub2:('a <a href="CCRandom.html#TYPEt">t</a> -> 'a <a href="CCRandom.html#TYPEt">t</a> -> 'a <a href="CCRandom.html#TYPEt">t</a>) list -><br> ?subn:(int <a href="CCRandom.html#TYPEt">t</a> * ('a list <a href="CCRandom.html#TYPEt">t</a> -> 'a <a href="CCRandom.html#TYPEt">t</a>)) list -><br> base:'a <a href="CCRandom.html#TYPEt">t</a> -> int <a href="CCRandom.html#TYPEt">t</a> -> 'a <a href="CCRandom.html#TYPEt">t</a></code></pre><div class="info ">
Recursion combinators, for building recursive values.
The integer generator is used to provide fuel. The <code class="code">sub_</code> generators
should use their arguments only once!<br>
</div>
<div class="param_info"><code class="code">sub1</code> : cases that recurse on one value</div>
<div class="param_info"><code class="code">sub2</code> : cases that use the recursive gen twice</div>
<div class="param_info"><code class="code">subn</code> : cases that use a list of recursive cases</div>
<br>
<h6 id="6_Applicative">Applicative</h6><br>
<pre><span id="VALpure"><span class="keyword">val</span> pure</span> : <code class="type">'a -> 'a <a href="CCRandom.html#TYPEt">t</a></code></pre>
<pre><span id="VAL(<*>)"><span class="keyword">val</span> (&lt;*&gt;)</span> : <code class="type">('a -> 'b) <a href="CCRandom.html#TYPEt">t</a> -> 'a <a href="CCRandom.html#TYPEt">t</a> -> 'b <a href="CCRandom.html#TYPEt">t</a></code></pre><br>
<h6 id="6_Runagenerator">Run a generator</h6><br>
<pre><span id="VALrun"><span class="keyword">val</span> run</span> : <code class="type">?st:<a href="CCRandom.html#TYPEstate">state</a> -> 'a <a href="CCRandom.html#TYPEt">t</a> -> 'a</code></pre><div class="info ">
Using a random state (possibly the one in argument) run a generator<br>
</div>
</body></html>