| <html lang="en"> |
| <head> |
| <title>rand - Untitled</title> |
| <meta http-equiv="Content-Type" content="text/html"> |
| <meta name="description" content="Untitled"> |
| <meta name="generator" content="makeinfo 4.13"> |
| <link title="Top" rel="start" href="index.html#Top"> |
| <link rel="up" href="Stdlib.html#Stdlib" title="Stdlib"> |
| <link rel="prev" href="qsort.html#qsort" title="qsort"> |
| <link rel="next" href="rand48.html#rand48" title="rand48"> |
| <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> |
| <meta http-equiv="Content-Style-Type" content="text/css"> |
| <style type="text/css"><!-- |
| pre.display { font-family:inherit } |
| pre.format { font-family:inherit } |
| pre.smalldisplay { font-family:inherit; font-size:smaller } |
| pre.smallformat { font-family:inherit; font-size:smaller } |
| pre.smallexample { font-size:smaller } |
| pre.smalllisp { font-size:smaller } |
| span.sc { font-variant:small-caps } |
| span.roman { font-family:serif; font-weight:normal; } |
| span.sansserif { font-family:sans-serif; font-weight:normal; } |
| --></style> |
| </head> |
| <body> |
| <div class="node"> |
| <a name="rand"></a> |
| <p> |
| Next: <a rel="next" accesskey="n" href="rand48.html#rand48">rand48</a>, |
| Previous: <a rel="previous" accesskey="p" href="qsort.html#qsort">qsort</a>, |
| Up: <a rel="up" accesskey="u" href="Stdlib.html#Stdlib">Stdlib</a> |
| <hr> |
| </div> |
| |
| <h3 class="section">2.32 <code>rand</code>, <code>srand</code>—pseudo-random numbers</h3> |
| |
| <p><a name="index-rand-66"></a><a name="index-srand-67"></a><a name="index-rand_005fr-68"></a><strong>Synopsis</strong> |
| <pre class="example"> #include <stdlib.h> |
| int rand(void); |
| void srand(unsigned int <var>seed</var>); |
| int rand_r(unsigned int *<var>seed</var>); |
| |
| </pre> |
| <p><strong>Description</strong><br> |
| <code>rand</code> returns a different integer each time it is called; each |
| integer is chosen by an algorithm designed to be unpredictable, so |
| that you can use <code>rand</code> when you require a random number. |
| The algorithm depends on a static variable called the “random seed”; |
| starting with a given value of the random seed always produces the |
| same sequence of numbers in successive calls to <code>rand</code>. |
| |
| <p>You can set the random seed using <code>srand</code>; it does nothing beyond |
| storing its argument in the static variable used by <code>rand</code>. You can |
| exploit this to make the pseudo-random sequence less predictable, if |
| you wish, by using some other unpredictable value (often the least |
| significant parts of a time-varying value) as the random seed before |
| beginning a sequence of calls to <code>rand</code>; or, if you wish to ensure |
| (for example, while debugging) that successive runs of your program |
| use the same “random” numbers, you can use <code>srand</code> to set the same |
| random seed at the outset. |
| |
| <p><br> |
| <strong>Returns</strong><br> |
| <code>rand</code> returns the next pseudo-random integer in sequence; it is a |
| number between <code>0</code> and <code>RAND_MAX</code> (inclusive). |
| |
| <p><code>srand</code> does not return a result. |
| |
| <p><br> |
| <strong>Portability</strong><br> |
| <code>rand</code> is required by ANSI, but the algorithm for pseudo-random |
| number generation is not specified; therefore, even if you use |
| the same random seed, you cannot expect the same sequence of results |
| on two different systems. |
| |
| <p><code>rand</code> requires no supporting OS subroutines. |
| |
| <p><br> |
| |
| </body></html> |
| |