moonpool/dev/ocaml/Stdlib/Random/index.html
2023-08-29 18:39:53 +00:00

2 lines
No EOL
12 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Random (ocaml.Stdlib.Random)</title><link rel="stylesheet" href="../../../_odoc-theme/odoc.css"/><meta charset="utf-8"/><meta name="generator" content="odoc 2.2.1"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><script src="../../../highlight.pack.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body class="odoc"><nav class="odoc-nav"><a href="../index.html">Up</a> <a href="../../index.html">ocaml</a> &#x00BB; <a href="../index.html">Stdlib</a> &#x00BB; Random</nav><header class="odoc-preamble"><h1>Module <code><span>Stdlib.Random</span></code></h1><p>Pseudo-random number generators (PRNG).</p><p>With multiple domains, each domain has its own generator that evolves independently of the generators of other domains. When a domain is created, its generator is initialized by splitting the state of the generator associated with the parent domain.</p><p>In contrast, all threads within a domain share the same domain-local generator. Independent generators can be created with the <a href="#val-split"><code>Random.split</code></a> function and used with the functions from the <a href="State/index.html"><code>Random.State</code></a> module.</p></header><nav class="odoc-toc"><ul><li><a href="#basic-functions">Basic functions</a></li><li><a href="#advanced-functions">Advanced functions</a></li></ul></nav><div class="odoc-content"><h2 id="basic-functions"><a href="#basic-functions" class="anchor"></a>Basic functions</h2><div class="odoc-spec"><div class="spec value anchored" id="val-init"><a href="#val-init" class="anchor"></a><code><span><span class="keyword">val</span> init : <span>int <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Initialize the domain-local generator, using the argument as a seed. The same seed will always yield the same sequence of numbers.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-full_init"><a href="#val-full_init" class="anchor"></a><code><span><span class="keyword">val</span> full_init : <span><span>int array</span> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Same as <a href="#val-init"><code>Random.init</code></a> but takes more data as seed.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-self_init"><a href="#val-self_init" class="anchor"></a><code><span><span class="keyword">val</span> self_init : <span>unit <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Initialize the domain-local generator with a random seed chosen in a system-dependent way. If <code>/dev/urandom</code> 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, domain-local state).</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-bits"><a href="#val-bits" class="anchor"></a><code><span><span class="keyword">val</span> bits : <span>unit <span class="arrow">&#45;&gt;</span></span> int</span></code></div><div class="spec-doc"><p>Return 30 random bits in a nonnegative integer.</p><ul class="at-tags"><li class="before"><span class="at-tag">before</span> <span class="value">5.0.0</span> <p>used a different algorithm (affects all the following functions)</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-int"><a href="#val-int" class="anchor"></a><code><span><span class="keyword">val</span> int : <span>int <span class="arrow">&#45;&gt;</span></span> int</span></code></div><div class="spec-doc"><p><code>Random.int bound</code> returns a random integer between 0 (inclusive) and <code>bound</code> (exclusive). <code>bound</code> must be greater than 0 and less than 2<sup>30</sup>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-full_int"><a href="#val-full_int" class="anchor"></a><code><span><span class="keyword">val</span> full_int : <span>int <span class="arrow">&#45;&gt;</span></span> int</span></code></div><div class="spec-doc"><p><code>Random.full_int bound</code> returns a random integer between 0 (inclusive) and <code>bound</code> (exclusive). <code>bound</code> may be any positive integer.</p><p>If <code>bound</code> is less than 2<sup>30</sup>, <code>Random.full_int bound</code> is equal to <a href="#val-int"><code>Random.int</code></a><code> bound</code>. If <code>bound</code> is greater than 2<sup>30</sup> (on 64-bit systems or non-standard environments, such as JavaScript), <code>Random.full_int</code> returns a value, where <a href="#val-int"><code>Random.int</code></a> raises <a href="../index.html#exception-Invalid_argument"><code>Stdlib.Invalid_argument</code></a>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.13.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-int32"><a href="#val-int32" class="anchor"></a><code><span><span class="keyword">val</span> int32 : <span><a href="../Int32/index.html#type-t">Int32.t</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Int32/index.html#type-t">Int32.t</a></span></code></div><div class="spec-doc"><p><code>Random.int32 bound</code> returns a random integer between 0 (inclusive) and <code>bound</code> (exclusive). <code>bound</code> must be greater than 0.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-nativeint"><a href="#val-nativeint" class="anchor"></a><code><span><span class="keyword">val</span> nativeint : <span><a href="../Nativeint/index.html#type-t">Nativeint.t</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Nativeint/index.html#type-t">Nativeint.t</a></span></code></div><div class="spec-doc"><p><code>Random.nativeint bound</code> returns a random integer between 0 (inclusive) and <code>bound</code> (exclusive). <code>bound</code> must be greater than 0.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-int64"><a href="#val-int64" class="anchor"></a><code><span><span class="keyword">val</span> int64 : <span><a href="../Int64/index.html#type-t">Int64.t</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Int64/index.html#type-t">Int64.t</a></span></code></div><div class="spec-doc"><p><code>Random.int64 bound</code> returns a random integer between 0 (inclusive) and <code>bound</code> (exclusive). <code>bound</code> must be greater than 0.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-float"><a href="#val-float" class="anchor"></a><code><span><span class="keyword">val</span> float : <span>float <span class="arrow">&#45;&gt;</span></span> float</span></code></div><div class="spec-doc"><p><code>Random.float bound</code> returns a random floating-point number between 0 and <code>bound</code> (inclusive). If <code>bound</code> is negative, the result is negative or zero. If <code>bound</code> is 0, the result is 0.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-bool"><a href="#val-bool" class="anchor"></a><code><span><span class="keyword">val</span> bool : <span>unit <span class="arrow">&#45;&gt;</span></span> bool</span></code></div><div class="spec-doc"><p><code>Random.bool ()</code> returns <code>true</code> or <code>false</code> with probability 0.5 each.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-bits32"><a href="#val-bits32" class="anchor"></a><code><span><span class="keyword">val</span> bits32 : <span>unit <span class="arrow">&#45;&gt;</span></span> <a href="../Int32/index.html#type-t">Int32.t</a></span></code></div><div class="spec-doc"><p><code>Random.bits32 ()</code> returns 32 random bits as an integer between <a href="../Int32/index.html#val-min_int"><code>Int32.min_int</code></a> and <a href="../Int32/index.html#val-max_int"><code>Int32.max_int</code></a>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.14.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-bits64"><a href="#val-bits64" class="anchor"></a><code><span><span class="keyword">val</span> bits64 : <span>unit <span class="arrow">&#45;&gt;</span></span> <a href="../Int64/index.html#type-t">Int64.t</a></span></code></div><div class="spec-doc"><p><code>Random.bits64 ()</code> returns 64 random bits as an integer between <a href="../Int64/index.html#val-min_int"><code>Int64.min_int</code></a> and <a href="../Int64/index.html#val-max_int"><code>Int64.max_int</code></a>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.14.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-nativebits"><a href="#val-nativebits" class="anchor"></a><code><span><span class="keyword">val</span> nativebits : <span>unit <span class="arrow">&#45;&gt;</span></span> <a href="../Nativeint/index.html#type-t">Nativeint.t</a></span></code></div><div class="spec-doc"><p><code>Random.nativebits ()</code> returns 32 or 64 random bits (depending on the bit width of the platform) as an integer between <a href="../Nativeint/index.html#val-min_int"><code>Nativeint.min_int</code></a> and <a href="../Nativeint/index.html#val-max_int"><code>Nativeint.max_int</code></a>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.14.0</li></ul></div></div><h2 id="advanced-functions"><a href="#advanced-functions" class="anchor"></a>Advanced functions</h2><p>The functions from module <a href="State/index.html"><code>State</code></a> 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.</p><div class="odoc-spec"><div class="spec module anchored" id="module-State"><a href="#module-State" class="anchor"></a><code><span><span class="keyword">module</span> <a href="State/index.html">State</a></span><span> : <span class="keyword">sig</span> ... <span class="keyword">end</span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get_state"><a href="#val-get_state" class="anchor"></a><code><span><span class="keyword">val</span> get_state : <span>unit <span class="arrow">&#45;&gt;</span></span> <a href="State/index.html#type-t">State.t</a></span></code></div><div class="spec-doc"><p><code>get_state()</code> returns a fresh copy of the current state of the domain-local generator (which is used by the basic functions).</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set_state"><a href="#val-set_state" class="anchor"></a><code><span><span class="keyword">val</span> set_state : <span><a href="State/index.html#type-t">State.t</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p><code>set_state s</code> updates the current state of the domain-local generator (which is used by the basic functions) by copying the state <code>s</code> into it.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-split"><a href="#val-split" class="anchor"></a><code><span><span class="keyword">val</span> split : <span>unit <span class="arrow">&#45;&gt;</span></span> <a href="State/index.html#type-t">State.t</a></span></code></div><div class="spec-doc"><p>Draw a fresh PRNG state from the current state of the domain-local generator used by the default functions. (The state of the domain-local generator is modified.) See <a href="State/index.html#val-split"><code>Random.State.split</code></a>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 5.0.0</li></ul></div></div></div></body></html>