summaryrefslogtreecommitdiff
path: root/devdocs/c/numeric%2Frandom%2Frand.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/c/numeric%2Frandom%2Frand.html')
-rw-r--r--devdocs/c/numeric%2Frandom%2Frand.html46
1 files changed, 46 insertions, 0 deletions
diff --git a/devdocs/c/numeric%2Frandom%2Frand.html b/devdocs/c/numeric%2Frandom%2Frand.html
new file mode 100644
index 00000000..cd6aaf4c
--- /dev/null
+++ b/devdocs/c/numeric%2Frandom%2Frand.html
@@ -0,0 +1,46 @@
+ <h1 id="firstHeading" class="firstHeading">rand</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;stdlib.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">int rand();</pre>
+</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> </table> <p>Returns a pseudo-random integer value between <code>​0​</code> and <code><a href="rand_max" title="c/numeric/random/RAND MAX">RAND_MAX</a></code> (<code>0</code> and <code>RAND_MAX</code> included).</p>
+<p><code><a href="srand" title="c/numeric/random/srand">srand()</a></code> seeds the pseudo-random number generator used by <code>rand()</code>. If <code>rand()</code> is used before any calls to <code>srand()</code>, <code>rand()</code> behaves as if it was seeded with <code><a href="http://en.cppreference.com/w/c/numeric/random/srand"><span class="kw740">srand</span></a><span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span></code>. Each time <code>rand()</code> is seeded with <code>srand()</code>, it must produce the same sequence of values.</p>
+<p><code>rand()</code> is not guaranteed to be thread-safe.</p>
+<h3 id="Parameters"> Parameters</h3> <p>(none)</p>
+<h3 id="Return_value"> Return value</h3> <p>Pseudo-random integer value between <code>​0​</code> and <code><a href="rand_max" title="c/numeric/random/RAND MAX">RAND_MAX</a></code>, inclusive.</p>
+<h3 id="Notes"> Notes</h3> <p>There are no guarantees as to the quality of the random sequence produced. In the past, some implementations of <code>rand()</code> have had serious shortcomings in the randomness, distribution and period of the sequence produced (in one well-known example, the low-order bit simply alternated between <code>1</code> and <code>0</code> between calls). <code>rand()</code> is not recommended for serious random-number generation needs, like cryptography.</p>
+<p>POSIX requires that the period of the pseudo-random number generator used by <code>rand</code> be at least 2<sup class="t-su">32</sup>.</p>
+<p>POSIX offered a thread-safe version of rand called <code>rand_r</code>, which is obsolete in favor of the <a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/drand48.html"><code>drand48</code></a> family of functions.</p>
+<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
+#include &lt;stdlib.h&gt;
+#include &lt;time.h&gt;
+
+int main(void)
+{
+ srand(time(NULL)); // use current time as seed for random generator
+ int random_variable = rand();
+ printf("Random value on [0,%d]: %d\n", RAND_MAX, random_variable);
+
+ // roll a 6-sided die 20 times
+ for (int n=0; n != 20; ++n) {
+ int x = 7;
+ while(x &gt; 6)
+ x = 1 + rand()/((RAND_MAX + 1u)/6); // Note: 1+rand()%6 is biased
+ printf("%d ", x);
+ }
+}</pre></div> <p>Possible output:</p>
+<div class="text source-text"><pre data-language="c">Random value on [0,2147483647]: 448749574
+3 1 3 1 4 2 2 1 3 6 4 4 3 1 6 2 3 2 6 1</pre></div> </div> <h3 id="References"> References</h3> <ul>
+<li> C17 standard (ISO/IEC 9899:2018): </li>
+<ul><li> 7.22.2.1 The rand function (p: 252) </li></ul>
+<li> C11 standard (ISO/IEC 9899:2011): </li>
+<ul><li> 7.22.2.1 The rand function (p: 346) </li></ul>
+<li> C99 standard (ISO/IEC 9899:1999): </li>
+<ul><li> 7.20.2.1 The rand function (p: 312) </li></ul>
+<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
+<ul><li> 4.10.2.1 The rand function </li></ul>
+</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="srand" title="c/numeric/random/srand"> <span class="t-lines"><span>srand</span></span></a></div> </td> <td> seeds pseudo-random number generator <br> <span class="t-mark">(function)</span> </td>
+</tr> <tr class="t-dsc"> <td> <div><a href="rand_max" title="c/numeric/random/RAND MAX"> <span class="t-lines"><span>RAND_MAX</span></span></a></div> </td> <td> maximum possible value generated by <code>rand()</code> <br> <span class="t-mark">(macro constant)</span> </td>
+</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/numeric/random/rand" title="cpp/numeric/random/rand">C++ documentation</a></span> for <code>rand</code> </td>
+</tr> </table> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
+ <a href="https://en.cppreference.com/w/c/numeric/random/rand" class="_attribution-link">https://en.cppreference.com/w/c/numeric/random/rand</a>
+ </p>
+</div>