⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rand.html

📁 posix标准英文,html格式
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2004 IEEE and The Open Group, All Rights Reserved --><title>rand</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="rand"></a> <a name="tag_03_592"></a><!-- rand --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2004 Edition<br>Copyright &copy; 2001-2004 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_592_01"></a>NAME</h4><blockquote>rand, rand_r, srand - pseudo-random number generator</blockquote><h4><a name="tag_03_592_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><p><code><tt>#include &lt;<a href="../basedefs/stdlib.h.html">stdlib.h</a>&gt;<br><br> int rand(void);<br></tt></code></p><div class="box"><code><tt><sup>[<a href="javascript:open_code('TSF')">TSF</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> int rand_r(unsigned *</tt><i>seed</i><tt>); <img src="../images/opt-end.gif" alt="[Option End]"border="0"></tt></code></div><tt><br> void srand(unsigned</tt> <i>seed</i><tt>);<br></tt></blockquote><h4><a name="tag_03_592_03"></a>DESCRIPTION</h4><blockquote><div class="box"><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]"border="0"> The functionality described on this reference page is aligned with the ISO&nbsp;C standard. Any conflict between therequirements described here and the ISO&nbsp;C standard is unintentional. This volume of IEEE&nbsp;Std&nbsp;1003.1-2001 defers tothe ISO&nbsp;C standard. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></div><p>The <i>rand</i>() function shall compute a sequence of pseudo-random integers in the range [0, {RAND_MAX}] <sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> &nbsp;with aperiod of at least 2<sup><small>32</small></sup>. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p><p><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> The<i>rand</i>() function need not be reentrant. A function that is not required to be reentrant is not required to be thread-safe.<img src="../images/opt-end.gif" alt="[Option End]" border="0"></p><p><sup>[<a href="javascript:open_code('TSF')">TSF</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">The <i>rand_r</i>() function shall compute a sequence of pseudo-random integers in the range [0, {RAND_MAX}]. (The value of the{RAND_MAX} macro shall be at least 32767.)</p><p>If <i>rand_r</i>() is called with the same initial value for the object pointed to by <i>seed</i> and that object is notmodified between successive returns and calls to <i>rand_r</i>(), the same sequence shall be generated. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p><p>The <i>srand</i>() function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequentcalls to <i>rand</i>(). If <i>srand</i>() is then called with the same seed value, the sequence of pseudo-random numbers shall berepeated. If <i>rand</i>() is called before any calls to <i>srand</i>() are made, the same sequence shall be generated as when<i>srand</i>() is first called with a seed value of 1.</p><p>The implementation shall behave as if no function defined in this volume of IEEE&nbsp;Std&nbsp;1003.1-2001 calls <i>rand</i>()or <i>srand</i>().</p></blockquote><h4><a name="tag_03_592_04"></a>RETURN VALUE</h4><blockquote><p>The <i>rand</i>() function shall return the next pseudo-random number in the sequence.</p><p><sup>[<a href="javascript:open_code('TSF')">TSF</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">The <i>rand_r</i>() function shall return a pseudo-random integer. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p><p>The <i>srand</i>() function shall not return a value.</p></blockquote><h4><a name="tag_03_592_05"></a>ERRORS</h4><blockquote><p>No errors are defined.</p></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_592_06"></a>EXAMPLES</h4><blockquote><h5><a name="tag_03_592_06_01"></a>Generating a Pseudo-Random Number Sequence</h5><p>The following example demonstrates how to generate a sequence of pseudo-random numbers.</p><pre><tt>#include &lt;stdio.h&gt;#include &lt;stdlib.h&gt;...    long count, i;    char *keystr;    int elementlen, len;    char c;.../* Initial random number generator. */    srand(1);<br>    /* Create keys using only lowercase characters */    len = 0;    for (i=0; i&lt;count; i++) {        while (len &lt; elementlen) {            c = (char) (rand() % 128);            if (islower(c))                keystr[len++] = c;        }<br>        keystr[len] = '\0';        printf("%s Element%0*ld\n", keystr, elementlen, i);        len = 0;    }</tt></pre><h5><a name="tag_03_592_06_02"></a>Generating the Same Sequence on Different Machines</h5><p>The following code defines a pair of functions that could be incorporated into applications wishing to ensure that the samesequence of numbers is generated across different machines.</p><pre><tt>static unsigned long next = 1;int myrand(void)  /* RAND_MAX assumed to be 32767. */{    next = next * 1103515245 + 12345;    return((unsigned)(next/65536) % 32768);}<br>void mysrand(unsigned seed){    next = seed;}</tt></pre></blockquote><h4><a name="tag_03_592_07"></a>APPLICATION USAGE</h4><blockquote><p>The <a href="../functions/drand48.html"><i>drand48</i>()</a> function provides a much more elaborate random numbergenerator.</p><p>The limitations on the amount of state that can be carried between one function call and another mean the <i>rand_r</i>()function can never be implemented in a way which satisfies all of the requirements on a pseudo-random number generator. Thereforethis function should be avoided whenever non-trivial requirements (including safety) have to be fulfilled.</p></blockquote><h4><a name="tag_03_592_08"></a>RATIONALE</h4><blockquote><p>The ISO&nbsp;C standard <i>rand</i>() and <i>srand</i>() functions allow per-process pseudo-random streams shared by allthreads. Those two functions need not change, but there has to be mutual-exclusion that prevents interference between two threadsconcurrently accessing the random number generator.</p><p>With regard to <i>rand</i>(), there are two different behaviors that may be wanted in a multi-threaded program:</p><ol><li><p>A single per-process sequence of pseudo-random numbers that is shared by all threads that call <i>rand</i>()</p></li><li><p>A different sequence of pseudo-random numbers for each thread that calls <i>rand</i>()</p></li></ol><p>This is provided by the modified thread-safe function based on whether the seed value is global to the entire process or localto each thread.</p><p>This does not address the known deficiencies of the <i>rand</i>() function implementations, which have been approached bymaintaining more state. In effect, this specifies new thread-safe forms of a deficient function.</p></blockquote><h4><a name="tag_03_592_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_592_10"></a>SEE ALSO</h4><blockquote><p><a href="drand48.html"><i>drand48</i>()</a>, the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/stdlib.h.html"><i>&lt;stdlib.h&gt;</i></a></p></blockquote><h4><a name="tag_03_592_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 1. Derived from Issue 1 of the SVID.</p></blockquote><h4><a name="tag_03_592_12"></a>Issue 5</h4><blockquote><p>The <i>rand_r</i>() function is included for alignment with the POSIX Threads Extension.</p><p>A note indicating that the <i>rand</i>() function need not be reentrant is added to the DESCRIPTION.</p></blockquote><h4><a name="tag_03_592_13"></a>Issue 6</h4><blockquote><p>Extensions beyond the ISO&nbsp;C standard are marked.</p><p>The <i>rand_r</i>() function is marked as part of the Thread-Safe Functions option.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX &reg; is a registered Trademark of The Open Group.<br>POSIX &reg; is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -