302 lines
6.8 KiB
HTML
302 lines
6.8 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of RANDOM</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>RANDOM</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"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
random, srandom, initstate, setstate - random number generator
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>></B>
|
|
|
|
<B>long int random(void);</B>
|
|
|
|
<B>void srandom(unsigned int </B><I>seed</I><B>);</B>
|
|
|
|
<B>char *initstate(unsigned int </B><I>seed</I><B>, char *</B><I>state</I><B>, size_t </B><I>n</I><B>);</B>
|
|
|
|
<B>char *setstate(char *</B><I>state</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>random</B>(),
|
|
|
|
<B>srandom</B>(),
|
|
|
|
<B>initstate</B>(),
|
|
|
|
<B>setstate</B>():
|
|
|
|
<DL COMPACT><DT id="1"><DD>
|
|
_XOPEN_SOURCE >= 500
|
|
|
|
<BR> || /* Glibc since 2.19: */ _DEFAULT_SOURCE
|
|
<BR> || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
|
|
</DL>
|
|
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>random</B>()
|
|
|
|
function uses a nonlinear additive feedback random
|
|
number generator employing a default table of size 31 long integers to
|
|
return successive pseudo-random numbers in
|
|
the range from 0 to <B>RAND_MAX</B>.
|
|
The period of this random number generator is very large, approximately
|
|
<I>16 * ((2^31) - 1)</I>.
|
|
|
|
<P>
|
|
|
|
The
|
|
<B>srandom</B>()
|
|
|
|
function sets its argument as the seed for a new
|
|
sequence of pseudo-random integers to be returned by
|
|
<B>random</B>().
|
|
|
|
These sequences are repeatable by calling
|
|
<B>srandom</B>()
|
|
|
|
with the same
|
|
seed value.
|
|
If no seed value is provided, the
|
|
<B>random</B>()
|
|
|
|
function
|
|
is automatically seeded with a value of 1.
|
|
<P>
|
|
|
|
The
|
|
<B>initstate</B>()
|
|
|
|
function allows a state array <I>state</I> to
|
|
be initialized for use by
|
|
<B>random</B>().
|
|
|
|
The size of the state array
|
|
<I>n</I> is used by
|
|
<B>initstate</B>()
|
|
|
|
to decide how sophisticated a
|
|
random number generator it should use---the larger the state array,
|
|
the better the random numbers will be.
|
|
Current "optimal" values for the size of the state array <I>n</I> are
|
|
8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
|
|
the nearest known amount.
|
|
Using less than 8 bytes results in an error.
|
|
<I>seed</I> is the seed for the
|
|
initialization, which specifies a starting point for the random number
|
|
sequence, and provides for restarting at the same point.
|
|
<P>
|
|
|
|
The
|
|
<B>setstate</B>()
|
|
|
|
function changes the state array used by the
|
|
<B>random</B>()
|
|
|
|
function.
|
|
The state array <I>state</I> is used for
|
|
random number generation until the next call to
|
|
<B>initstate</B>()
|
|
|
|
or
|
|
<B>setstate</B>().
|
|
|
|
<I>state</I> must first have been initialized
|
|
using
|
|
<B>initstate</B>()
|
|
|
|
or be the result of a previous call of
|
|
<B>setstate</B>().
|
|
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
The
|
|
<B>random</B>()
|
|
|
|
function returns a value between 0 and
|
|
<B>RAND_MAX</B>.
|
|
|
|
The
|
|
<B>srandom</B>()
|
|
|
|
function returns no value.
|
|
<P>
|
|
|
|
The
|
|
<B>initstate</B>()
|
|
|
|
function returns a pointer to the previous state array.
|
|
On error,
|
|
<I>errno</I>
|
|
|
|
is set to indicate the cause.
|
|
<P>
|
|
|
|
On success,
|
|
<B>setstate</B>()
|
|
|
|
returns a pointer to the previous state array.
|
|
On error, it returns NULL, with
|
|
<I>errno</I>
|
|
|
|
set to indicate the cause of the error.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
<DL COMPACT>
|
|
<DT id="2"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
The
|
|
<I>state</I>
|
|
|
|
argument given to
|
|
<B>setstate</B>()
|
|
|
|
was NULL.
|
|
<DT id="3"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
A state array of less than 8 bytes was specified to
|
|
<B>initstate</B>().
|
|
|
|
</DL>
|
|
<A NAME="lbAG"> </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>random</B>(),
|
|
|
|
<B>srandom</B>(),
|
|
|
|
<BR>
|
|
|
|
<B>initstate</B>(),
|
|
|
|
<B>setstate</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<A NAME="lbAH"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008, 4.3BSD.
|
|
<A NAME="lbAI"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
The
|
|
<B>random</B>()
|
|
|
|
function should not be used in multithreaded programs
|
|
where reproducible behavior is required.
|
|
Use
|
|
<B><A HREF="/cgi-bin/man/man2html?3+random_r">random_r</A></B>(3)
|
|
|
|
for that purpose.
|
|
<P>
|
|
|
|
Random-number generation is a complex topic.
|
|
<I>Numerical Recipes in C: The Art of Scientific Computing</I>
|
|
|
|
(William H. Press, Brian P. Flannery, Saul A. Teukolsky, William
|
|
T. Vetterling; New York: Cambridge University Press, 2007, 3rd ed.)
|
|
provides an excellent discussion of practical random-number generation
|
|
issues in Chapter 7 (Random Numbers).
|
|
<P>
|
|
|
|
For a more theoretical discussion which also covers many practical issues
|
|
in depth, see Chapter 3 (Random Numbers) in Donald E. Knuth's
|
|
<I>The Art of Computer Programming</I>,
|
|
|
|
volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts:
|
|
Addison-Wesley Publishing Company, 1981.
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>BUGS</H2>
|
|
|
|
According to POSIX,
|
|
<B>initstate</B>()
|
|
|
|
should return NULL on error.
|
|
In the glibc implementation,
|
|
<I>errno</I>
|
|
|
|
is (as specified) set on error, but the function does not return NULL.
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+getrandom">getrandom</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+drand48">drand48</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+rand">rand</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+random_r">random_r</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+srand">srand</A></B>(3)
|
|
|
|
<A NAME="lbAL"> </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"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="4"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="5"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="6"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="7"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="8"><A HREF="#lbAF">ERRORS</A><DD>
|
|
<DT id="9"><A HREF="#lbAG">ATTRIBUTES</A><DD>
|
|
<DT id="10"><A HREF="#lbAH">CONFORMING TO</A><DD>
|
|
<DT id="11"><A HREF="#lbAI">NOTES</A><DD>
|
|
<DT id="12"><A HREF="#lbAJ">BUGS</A><DD>
|
|
<DT id="13"><A HREF="#lbAK">SEE ALSO</A><DD>
|
|
<DT id="14"><A HREF="#lbAL">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:54 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|