Files
oldlinux-files/Ref-docs/POSIX/susv3/functions/initstate.html
2024-02-19 00:21:47 -05:00

187 lines
7.9 KiB
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 The Open Group, All Rights Reserved -->
<title>initstate</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
</script>
<basefont size="3"> <a name="initstate"></a> <a name="tag_03_280"></a><!-- initstate -->
<!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 6<br>
IEEE Std 1003.1-2001<br>
Copyright &copy; 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
<!--header end-->
<hr size="2" noshade>
<h4><a name="tag_03_280_01"></a>NAME</h4>
<blockquote>initstate, random, setstate, srandom - pseudo-random number functions</blockquote>
<h4><a name="tag_03_280_02"></a>SYNOPSIS</h4>
<blockquote class="synopsis">
<div class="box"><code><tt><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt=
"[Option Start]" border="0"> #include &lt;<a href="../basedefs/stdlib.h.html">stdlib.h</a>&gt;<br>
<br>
char *initstate(unsigned</tt> <i>seed</i><tt>, char *</tt><i>state</i><tt>, size_t</tt> <i>size</i><tt>);<br>
long random(void);<br>
char *setstate(const char *</tt><i>state</i><tt>);<br>
void srandom(unsigned</tt> <i>seed</i><tt>); <img src="../images/opt-end.gif" alt="[Option End]" border="0"></tt></code></div>
<tt><br>
</tt></blockquote>
<h4><a name="tag_03_280_03"></a>DESCRIPTION</h4>
<blockquote>
<p>The <i>random</i>() function shall use a non-linear additive feedback random-number generator employing a default state array
size of 31 <b>long</b> integers to return successive pseudo-random numbers in the range from 0 to 2<sup><small>31</small></sup>-1.
The period of this random-number generator is approximately 16 x (2<small><sup>31</sup></small>-1). The size of the state array
determines the period of the random-number generator. Increasing the state array size shall increase the period.</p>
<p>With 256 bytes of state information, the period of the random-number generator shall be greater than
2<small><sup>69</sup></small>.</p>
<p>Like <a href="../functions/rand.html"><i>rand</i>()</a>, <i>random</i>() shall produce by default a sequence of numbers that can
be duplicated by calling <i>srandom</i>() with 1 as the seed.</p>
<p>The <i>srandom</i>() function shall initialize the current state array using the value of <i>seed</i>.</p>
<p>The <i>initstate</i>() and <i>setstate</i>() functions handle restarting and changing random-number generators. The
<i>initstate</i>() function allows a state array, pointed to by the <i>state</i> argument, to be initialized for future use. The
<i>size</i> argument, which specifies the size in bytes of the state array, shall be used by <i>initstate</i>() to decide what type
of random-number generator to use; the larger the state array, the more random the numbers. Values for the amount of state
information are 8, 32, 64, 128, and 256 bytes. Other values greater than 8 bytes are rounded down to the nearest one of these
values. If <i>initstate</i>() is called with 8&lt;=<i>size</i>&lt;32, then <i>random</i>() shall use a simple linear congruential
random number generator. The <i>seed</i> argument specifies a starting point for the random-number sequence and provides for
restarting at the same point. The <i>initstate</i>() function shall return a pointer to the previous state information array.</p>
<p>If <i>initstate</i>() has not been called, then <i>random</i>() shall behave as though <i>initstate</i>() had been called with
<i>seed</i>=1 and <i>size</i>=128.</p>
<p>Once a state has been initialized, <i>setstate</i>() allows switching between state arrays. The array defined by the
<i>state</i> argument shall be used for further random-number generation until <i>initstate</i>() is called or <i>setstate</i>() is
called again. The <i>setstate</i>() function shall return a pointer to the previous state array.</p>
</blockquote>
<h4><a name="tag_03_280_04"></a>RETURN VALUE</h4>
<blockquote>
<p>If <i>initstate</i>() is called with <i>size</i> less than 8, it shall return NULL.</p>
<p>The <i>random</i>() function shall return the generated pseudo-random number.</p>
<p>The <i>srandom</i>() function shall not return a value.</p>
<p>Upon successful completion, <i>initstate</i>() and <i>setstate</i>() shall return a pointer to the previous state array;
otherwise, a null pointer shall be returned.</p>
</blockquote>
<h4><a name="tag_03_280_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_280_06"></a>EXAMPLES</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_280_07"></a>APPLICATION USAGE</h4>
<blockquote>
<p>After initialization, a state array can be restarted at a different point in one of two ways:</p>
<ol>
<li>
<p>The <i>initstate</i>() function can be used, with the desired seed, state array, and size of the array.</p>
</li>
<li>
<p>The <i>setstate</i>() function, with the desired state, can be used, followed by <i>srandom</i>() with the desired seed. The
advantage of using both of these functions is that the size of the state array does not have to be saved once it is
initialized.</p>
</li>
</ol>
<p>Although some implementations of <i>random</i>() have written messages to standard error, such implementations do not conform to
IEEE&nbsp;Std&nbsp;1003.1-2001.</p>
<p>Issue 5 restored the historical behavior of this function.</p>
<p>Threaded applications should use <a href="../functions/rand_r.html"><i>rand_r</i>()</a>, <a href=
"../functions/erand48.html"><i>erand48</i>()</a>, <a href="../functions/nrand48.html"><i>nrand48</i>()</a>, or <a href=
"../functions/jrand48.html"><i>jrand48</i>()</a> instead of <i>random</i>() when an independent random number sequence in multiple
threads is required.</p>
</blockquote>
<h4><a name="tag_03_280_08"></a>RATIONALE</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_280_09"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_280_10"></a>SEE ALSO</h4>
<blockquote>
<p><a href="drand48.html"><i>drand48</i>()</a> , <a href="rand.html"><i>rand</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_280_11"></a>CHANGE HISTORY</h4>
<blockquote>
<p>First released in Issue 4, Version 2.</p>
</blockquote>
<h4><a name="tag_03_280_12"></a>Issue 5</h4>
<blockquote>
<p>Moved from X/OPEN UNIX extension to BASE.</p>
<p>In the DESCRIPTION, the phrase &quot;values smaller than 8&quot; is replaced with &quot;values greater than or equal to 8, or less than
32&quot;, &quot;<i>size</i>&lt;8&quot; is replaced with &quot;8&lt;=<i>size</i> &lt;32&quot;, and a new first paragraph is added to the RETURN VALUE
section. A note is added to the APPLICATION USAGE indicating that these changes restore the historical behavior of the
function.</p>
</blockquote>
<h4><a name="tag_03_280_13"></a>Issue 6</h4>
<blockquote>
<p>In the DESCRIPTION, duplicate text &quot;For values greater than or equal to 8 ...&quot; is removed.</p>
</blockquote>
<div class="box"><em>End of informative text.</em></div>
<hr>
<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>