man-pages/man3/rand.3.html
2021-03-31 01:06:50 +01:00

313 lines
7.1 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of RAND</TITLE>
</HEAD><BODY>
<H1>RAND</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
rand, rand_r, srand - pseudo-random number generator
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;</B>
<B>int rand(void);</B>
<B>int rand_r(unsigned int *</B><I>seedp</I><B>);</B>
<B>void srand(unsigned int </B><I>seed</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>rand_r</B>():
<DL COMPACT><DT id="1"><DD>
Since glibc 2.24:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;_POSIX_C_SOURCE&nbsp;&gt;=&nbsp;199506L
<BR>
Glibc 2.23 and earlier
<BR>&nbsp;&nbsp;&nbsp;&nbsp;_POSIX_C_SOURCE
</DL>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>rand</B>()
function returns a pseudo-random integer in the range 0 to
<B>RAND_MAX</B>
inclusive (i.e., the mathematical range [0,&nbsp;<B>RAND_MAX</B>]).
<P>
The
<B>srand</B>()
function sets its argument as the seed for a new
sequence of pseudo-random integers to be returned by
<B>rand</B>().
These sequences are repeatable by calling
<B>srand</B>()
with the same seed value.
<P>
If no seed value is provided, the
<B>rand</B>()
function is automatically seeded with a value of 1.
<P>
The function
<B>rand</B>()
is not reentrant, since it
uses hidden state that is modified on each call.
This might just be the seed value to be used by the next call,
or it might be something more elaborate.
In order to get reproducible behavior in a threaded
application, this state must be made explicit;
this can be done using the reentrant function
<B>rand_r</B>().
<P>
Like
<B>rand</B>(),
<B>rand_r</B>()
returns a pseudo-random integer in the range [0,&nbsp;<B>RAND_MAX</B>].
The
<I>seedp</I>
argument is a pointer to an
<I>unsigned int</I>
that is used to store state between calls.
If
<B>rand_r</B>()
is called with the same initial value for the integer pointed to by
<I>seedp</I>,
and that value is not modified between calls,
then the same pseudo-random sequence will result.
<P>
The value pointed to by the
<I>seedp</I>
argument of
<B>rand_r</B>()
provides only a very small amount of state,
so this function will be a weak pseudo-random generator.
Try
<B><A HREF="/cgi-bin/man/man2html?3+drand48_r">drand48_r</A></B>(3)
instead.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
The
<B>rand</B>()
and
<B>rand_r</B>()
functions return a value between 0 and
<B>RAND_MAX</B>
(inclusive).
The
<B>srand</B>()
function returns no value.
<A NAME="lbAF">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>rand</B>(),
<B>rand_r</B>(),
<B>srand</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
The functions
<B>rand</B>()
and
<B>srand</B>()
conform to SVr4, 4.3BSD, C89, C99, POSIX.1-2001.
The function
<B>rand_r</B>()
is from POSIX.1-2001.
POSIX.1-2008 marks
<B>rand_r</B>()
as obsolete.
<A NAME="lbAH">&nbsp;</A>
<H2>NOTES</H2>
The versions of
<B>rand</B>()
and
<B>srand</B>()
in the Linux C Library use the same random number generator as
<B><A HREF="/cgi-bin/man/man2html?3+random">random</A></B>(3)
and
<B><A HREF="/cgi-bin/man/man2html?3+srandom">srandom</A></B>(3),
so the lower-order bits should be as random as the higher-order bits.
However, on older
<B>rand</B>()
implementations, and on current implementations on different systems,
the lower-order bits are much less random than the higher-order bits.
Do not use this function in applications intended to be portable
when good randomness is needed.
(Use
<B><A HREF="/cgi-bin/man/man2html?3+random">random</A></B>(3)
instead.)
<A NAME="lbAI">&nbsp;</A>
<H2>EXAMPLE</H2>
POSIX.1-2001 gives the following example of an implementation of
<B>rand</B>()
and
<B>srand</B>(),
possibly useful when one needs the same sequence on two different machines.
<P>
static unsigned long next = 1;
<P>
/* RAND_MAX assumed to be 32767 */
int myrand(void) {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;next&nbsp;=&nbsp;next&nbsp;*&nbsp;1103515245&nbsp;+&nbsp;12345;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;return((unsigned)(next/65536)&nbsp;%&nbsp;32768);
}
<P>
void mysrand(unsigned int seed) {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;next&nbsp;=&nbsp;seed;
}
<P>
The following program can be used to display the
pseudo-random sequence produced by
<B>rand</B>()
when given a particular seed.
<P>
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;j,&nbsp;r,&nbsp;nloops;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;int&nbsp;seed;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;!=&nbsp;3)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;Usage:&nbsp;%s&nbsp;&lt;seed&gt;&nbsp;&lt;nloops&gt;\n&quot;,&nbsp;argv[0]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;seed&nbsp;=&nbsp;atoi(argv[1]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;nloops&nbsp;=&nbsp;atoi(argv[2]);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;srand(seed);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(j&nbsp;=&nbsp;0;&nbsp;j&nbsp;&lt;&nbsp;nloops;&nbsp;j++)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;=&nbsp;&nbsp;rand();
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;%d\n&quot;,&nbsp;r);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAJ">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?3+drand48">drand48</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+random">random</A></B>(3)
<A NAME="lbAK">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="2"><A HREF="#lbAB">NAME</A><DD>
<DT id="3"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="4"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="5"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="6"><A HREF="#lbAF">ATTRIBUTES</A><DD>
<DT id="7"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="8"><A HREF="#lbAH">NOTES</A><DD>
<DT id="9"><A HREF="#lbAI">EXAMPLE</A><DD>
<DT id="10"><A HREF="#lbAJ">SEE ALSO</A><DD>
<DT id="11"><A HREF="#lbAK">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:53 GMT, March 31, 2021
</BODY>
</HTML>