diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/numeric%2Frandom%2Fsrand.html | |
new repository
Diffstat (limited to 'devdocs/c/numeric%2Frandom%2Fsrand.html')
| -rw-r--r-- | devdocs/c/numeric%2Frandom%2Fsrand.html | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/devdocs/c/numeric%2Frandom%2Fsrand.html b/devdocs/c/numeric%2Frandom%2Fsrand.html new file mode 100644 index 00000000..b51663b0 --- /dev/null +++ b/devdocs/c/numeric%2Frandom%2Fsrand.html @@ -0,0 +1,38 @@ + <h1 id="firstHeading" class="firstHeading">srand</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">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 <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); +}</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"> + © 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> |
