1<html lang="en"> 2<head> 3<title>rand - Untitled</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Untitled"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Stdlib.html#Stdlib" title="Stdlib"> 9<link rel="prev" href="qsort.html#qsort" title="qsort"> 10<link rel="next" href="rand48.html#rand48" title="rand48"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<meta http-equiv="Content-Style-Type" content="text/css"> 13<style type="text/css"><!-- 14 pre.display { font-family:inherit } 15 pre.format { font-family:inherit } 16 pre.smalldisplay { font-family:inherit; font-size:smaller } 17 pre.smallformat { font-family:inherit; font-size:smaller } 18 pre.smallexample { font-size:smaller } 19 pre.smalllisp { font-size:smaller } 20 span.sc { font-variant:small-caps } 21 span.roman { font-family:serif; font-weight:normal; } 22 span.sansserif { font-family:sans-serif; font-weight:normal; } 23--></style> 24<link rel="stylesheet" type="text/css" href="../cs.css"> 25</head> 26<body> 27<div class="node"> 28<a name="rand"></a> 29<p> 30Next: <a rel="next" accesskey="n" href="rand48.html#rand48">rand48</a>, 31Previous: <a rel="previous" accesskey="p" href="qsort.html#qsort">qsort</a>, 32Up: <a rel="up" accesskey="u" href="Stdlib.html#Stdlib">Stdlib</a> 33<hr> 34</div> 35 36<h3 class="section">2.32 <code>rand</code>, <code>srand</code>—pseudo-random numbers</h3> 37 38<p><a name="index-rand-66"></a><a name="index-srand-67"></a><a name="index-rand_005fr-68"></a><strong>Synopsis</strong> 39<pre class="example"> #include <stdlib.h> 40 int rand(void); 41 void srand(unsigned int <var>seed</var>); 42 int rand_r(unsigned int *<var>seed</var>); 43 44</pre> 45 <p><strong>Description</strong><br> 46<code>rand</code> returns a different integer each time it is called; each 47integer is chosen by an algorithm designed to be unpredictable, so 48that you can use <code>rand</code> when you require a random number. 49The algorithm depends on a static variable called the “random seed”; 50starting with a given value of the random seed always produces the 51same sequence of numbers in successive calls to <code>rand</code>. 52 53 <p>You can set the random seed using <code>srand</code>; it does nothing beyond 54storing its argument in the static variable used by <code>rand</code>. You can 55exploit this to make the pseudo-random sequence less predictable, if 56you wish, by using some other unpredictable value (often the least 57significant parts of a time-varying value) as the random seed before 58beginning a sequence of calls to <code>rand</code>; or, if you wish to ensure 59(for example, while debugging) that successive runs of your program 60use the same “random” numbers, you can use <code>srand</code> to set the same 61random seed at the outset. 62 63 <p><br> 64<strong>Returns</strong><br> 65<code>rand</code> returns the next pseudo-random integer in sequence; it is a 66number between <code>0</code> and <code>RAND_MAX</code> (inclusive). 67 68 <p><code>srand</code> does not return a result. 69 70 <p><br> 71<strong>Portability</strong><br> 72<code>rand</code> is required by ANSI, but the algorithm for pseudo-random 73number generation is not specified; therefore, even if you use 74the same random seed, you cannot expect the same sequence of results 75on two different systems. 76 77 <p><code>rand</code> requires no supporting OS subroutines. 78 79 <p><br> 80 81 </body></html> 82 83