summaryrefslogtreecommitdiff
path: root/devdocs/c/numeric%2Frandom%2Fsrand.html
blob: b51663b0dee01077c090646c78b7517fa56d74a0 (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
    <h1 id="firstHeading" class="firstHeading">srand</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">void srand( unsigned seed );</pre>
</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr>  </table> <p>Seeds the pseudo-random number generator used by <code><a href="rand" title="c/numeric/random/rand">rand()</a></code> with the value <code>seed</code>.</p>
<p>If <code>rand()</code> is used before any calls to <code>srand()</code>, <code>rand()</code> behaves as if it was seeded with <code>srand(1)</code>.</p>
<p>Each time <code>rand()</code> is seeded with the same <code>seed</code>, it must produce the same sequence of values.</p>
<p><code>srand()</code> is not guaranteed to be thread-safe.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> seed </td> <td> - </td> <td> the seed value </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>(none)</p>
<h3 id="Notes"> Notes</h3> <p>Generally speaking, the pseudo-random number generator should only be seeded once, before any calls to <code>rand()</code>, and the start of the program. It should not be repeatedly seeded, or reseeded every time you wish to generate a new batch of pseudo-random numbers.</p>
<p>Standard practice is to use the result of a call to <code><a href="http://en.cppreference.com/w/c/chrono/time"><span class="kw513">time</span></a><span class="br0">(</span><span class="nu0">0</span><span class="br0">)</span></code> as the seed. However, <code>time()</code> returns a <code><a href="../../chrono/time_t" title="c/chrono/time t">time_t</a></code> value, and <code>time_t</code> is not guaranteed to be an integral type. In practice, though, every major implementation defines <code>time_t</code> to be an integral type, and this is also what POSIX requires.</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);
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">Random value on [0 2147483647]: 1373858591</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 7.22.2.2 The srand function (p: 252-253) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.22.2.2 The srand function (p: 346-347) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.20.2.2 The srand function (p: 312-313) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.10.2.2 The srand function </li></ul>
</ul>               <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="rand" title="c/numeric/random/rand"> <span class="t-lines"><span>rand</span></span></a></div> </td> <td> generates a pseudo-random number <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><a href="http://en.cppreference.com/w/c/numeric/random/rand"><span class="kw740">rand</span></a><span class="br0">(</span><span class="br0">)</span></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/srand" title="cpp/numeric/random/srand">C++ documentation</a></span> for <code>srand</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/srand" class="_attribution-link">https://en.cppreference.com/w/c/numeric/random/srand</a>
  </p>
</div>