blob: cd6aaf4ccdc97cfe5ced4f64bc47b76fe98c1209 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
<h1 id="firstHeading" class="firstHeading">rand</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><stdlib.h></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 <stdio.h>
#include <stdlib.h>
#include <time.h>
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 > 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">
© 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>
|