Files
oldlinux-files/study/Ref-docs/POSIX/susv3/functions/drand48.html
2024-02-19 00:25:23 -05:00

201 lines
9.4 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>drand48</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
</script>
<basefont size="3"> <a name="drand48"></a> <a name="tag_03_113"></a><!-- drand48 -->
<!--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_113_01"></a>NAME</h4>
<blockquote>drand48, erand48, jrand48, lcong48, lrand48, mrand48, nrand48, seed48, srand48 - generate uniformly distributed
pseudo-random numbers</blockquote>
<h4><a name="tag_03_113_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>
double drand48(void);<br>
double erand48(unsigned short</tt> <i>xsubi</i><tt>[3]);<br>
long jrand48(unsigned short</tt> <i>xsubi</i><tt>[3]);<br>
void lcong48(unsigned short</tt> <i>param</i><tt>[7]);<br>
long lrand48(void);<br>
long mrand48(void);<br>
long nrand48(unsigned short</tt> <i>xsubi</i><tt>[3]);<br>
unsigned short *seed48(unsigned short</tt> <i>seed16v</i><tt>[3]);<br>
void srand48(long</tt> <i>seedval</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_113_03"></a>DESCRIPTION</h4>
<blockquote>
<p>This family of functions shall generate pseudo-random numbers using a linear congruential algorithm and 48-bit integer
arithmetic.</p>
<p>The <i>drand48</i>() and <i>erand48</i>() functions shall return non-negative, double-precision, floating-point values,
uniformly distributed over the interval [0.0,1.0).</p>
<p>The <i>lrand48</i>() and <i>nrand48</i>() functions shall return non-negative, long integers, uniformly distributed over the
interval [0,2<sup><small>31</small></sup>).</p>
<p>The <i>mrand48</i>() and <i>jrand48</i>() functions shall return signed long integers uniformly distributed over the interval
[-2<sup><small>31</small></sup>,2<sup><small>31</small></sup>).</p>
<p>The <i>srand48</i>(), <i>seed48</i>(), and <i>lcong48</i>() functions are initialization entry points, one of which should be
invoked before either <i>drand48</i>(), <i>lrand48</i>(), or <i>mrand48</i>() is called. (Although it is not recommended practice,
constant default initializer values shall be supplied automatically if <i>drand48</i>(), <i>lrand48</i>(), or <i>mrand48</i>() is
called without a prior call to an initialization entry point.) The <i>erand48</i>(), <i>nrand48</i>(), and <i>jrand48</i>()
functions do not require an initialization entry point to be called first.</p>
<p>All the routines work by generating a sequence of 48-bit integer values,
X<small><sub>i</sub></small> , according to the linear congruential
formula:</p>
<blockquote>
X<small><sub>n+1</sub></small> = (aX<small><sub>n</sub></small> + c)<small><sub>mod m</sub></small>&nbsp;&nbsp;&nbsp;n&gt;= 0
</blockquote>
<p>The parameter <i>m</i> = 2<small><sup>48</sup></small>; hence 48-bit integer arithmetic is performed. Unless <i>lcong48</i>() is invoked, the multiplier
value <i>a</i> and the addend value <i>c</i> are given by:</p>
<blockquote>
<i>a</i> = 5DEECE66D<small><sub>16</sub></small> = 273673163155<small><sub>8</sub></small>
<p>
<i>c</i> = B<small><sub>16</sub></small> = 13<small><sub>8</sub></small>
</blockquote>
<p>The value returned by any of the <i>drand48</i>(), <i>erand48</i>(), <i>jrand48</i>(), <i>lrand48</i>(), <i>mrand48</i>(), or
<i>nrand48</i>() functions is computed by first generating the next 48-bit X<small><sub>i</sub></small> in the sequence. Then the appropriate number
of bits, according to the type of data item to be returned, are copied from the high-order (leftmost) bits of X<small><sub>i</sub></small> and
transformed into the returned value.</p>
<p>The <i>drand48</i>(), <i>lrand48</i>(), and <i>mrand48</i>() functions store the last 48-bit X<small><sub>i</sub></small> generated in an internal
buffer; that is why the application shall ensure that these are initialized prior to being invoked. The <i>erand48</i>(),
<i>nrand48</i>(), and <i>jrand48</i>() functions require the calling program to provide storage for the successive X<small><sub>i</sub></small> values
in the array specified as an argument when the functions are invoked. That is why these routines do not have to be initialized; the
calling program merely has to place the desired initial value of X<small><sub>i</sub></small> into the array and pass it as an argument. By using
different arguments, <i>erand48</i>(), <i>nrand48</i>(), and <i>jrand48</i>() allow separate modules of a large program to generate
several <i>independent</i> streams of pseudo-random numbers; that is, the sequence of numbers in each stream shall <i>not</i>
depend upon how many times the routines are called to generate numbers for the other streams.</p>
<p>The initializer function <i>srand48</i>() sets the high-order 32 bits of X<small><sub>i</sub></small> to the low-order 32 bits contained in its
argument. The low-order 16 bits of X<small><sub>i</sub></small> are set to the arbitrary value 330E<small><sub>16</sub></small>.</p>
<p>The initializer function <i>seed48</i>() sets the value of X<small><sub>i</sub></small> to the 48-bit value specified in the argument array. The
low-order 16 bits of X<small><sub>i</sub></small> are set to the low-order 16 bits of <i>seed16v</i>[<i>0</i>]. The mid-order 16 bits of X<small><sub>i</sub></small> are
set to the low-order 16 bits of <i>seed16v</i>[<i>1</i>]. The high-order 16 bits of X<small><sub>i</sub></small> are set to the low-order 16 bits of
<i>seed16v</i>[<i>2</i>]. In addition, the previous value of X<small><sub>i</sub></small> is copied into a 48-bit internal buffer, used only by
<i>seed48</i>(), and a pointer to this buffer is the value returned by <i>seed48</i>(). This returned pointer, which can just be
ignored if not needed, is useful if a program is to be restarted from a given point at some future time-use the pointer to get at
and store the last X<small><sub>i</sub></small> value, and then use this value to reinitialize via <i>seed48</i>() when the program is restarted.</p>
<p>The initializer function <i>lcong48</i>() allows the user to specify the initial X<small><sub>i</sub></small> , the multiplier value <i>a</i>, and the
addend value <i>c</i>.
Argument array elements <i>param</i>[<i>0-2</i>] specify X<small><sub>i</sub></small> , <i>param</i>[<i>3-5</i>] specify the
multiplier <i>a</i>, and <i>param</i>[<i>6</i>] specifies the 16-bit addend <i>c</i>. After <i>lcong48</i>() is called, a subsequent call to
either <i>srand48</i>() or <i>seed48</i>() shall restore the standard multiplier and addend values, <i>a</i> and <i>c,</i>
specified above.</p>
<p>The <i>drand48</i>(), <i>lrand48</i>(), and <i>mrand48</i>() functions need not be reentrant. A function that is not required to
be reentrant is not required to be thread-safe.</p>
</blockquote>
<h4><a name="tag_03_113_04"></a>RETURN VALUE</h4>
<blockquote>
<p>As described in the DESCRIPTION above.</p>
</blockquote>
<h4><a name="tag_03_113_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_113_06"></a>EXAMPLES</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_113_07"></a>APPLICATION USAGE</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_113_08"></a>RATIONALE</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_113_09"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_113_10"></a>SEE ALSO</h4>
<blockquote>
<p><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_113_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_113_12"></a>Issue 5</h4>
<blockquote>
<p>A note indicating that these functions need not be reentrant is added to the DESCRIPTION.</p>
</blockquote>
<h4><a name="tag_03_113_13"></a>Issue 6</h4>
<blockquote>
<p>The DESCRIPTION is updated to avoid use of the term &quot;must&quot; for application requirements.<br>
</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>