290 lines
6.2 KiB
HTML
290 lines
6.2 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of RANDOM_R</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>RANDOM_R</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2017-09-15<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_r, srandom_r, initstate_r, setstate_r - reentrant
|
|
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>int random_r(struct random_data *</B><I>buf</I><B>, int32_t *</B><I>result</I><B>);</B>
|
|
|
|
<B>int srandom_r(unsigned int </B><I>seed</I><B>, struct random_data *</B><I>buf</I><B>);</B>
|
|
|
|
<B>int initstate_r(unsigned int </B><I>seed</I><B>, char *</B><I>statebuf</I><B>,</B>
|
|
<B> size_t </B><I>statelen</I><B>, struct random_data *</B><I>buf</I><B>);</B>
|
|
|
|
<B>int setstate_r(char *</B><I>statebuf</I><B>, struct random_data *</B><I>buf</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_r</B>(),
|
|
|
|
<B>srandom_r</B>(),
|
|
|
|
<B>initstate_r</B>(),
|
|
|
|
<B>setstate_r</B>():
|
|
|
|
<DL COMPACT><DT id="1"><DD>
|
|
/* Glibc since 2.19: */ _DEFAULT_SOURCE
|
|
<BR> || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
|
|
</DL>
|
|
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
These functions are the reentrant equivalents
|
|
of the functions described in
|
|
<B><A HREF="/cgi-bin/man/man2html?3+random">random</A></B>(3).
|
|
|
|
They are suitable for use in multithreaded programs where each thread
|
|
needs to obtain an independent, reproducible sequence of random numbers.
|
|
<P>
|
|
|
|
The
|
|
<B>random_r</B>()
|
|
|
|
function is like
|
|
<B><A HREF="/cgi-bin/man/man2html?3+random">random</A></B>(3),
|
|
|
|
except that instead of using state information maintained
|
|
in a global variable,
|
|
it uses the state information in the argument pointed to by
|
|
<I>buf</I>,
|
|
|
|
which must have been previously initialized by
|
|
<B>initstate_r</B>().
|
|
|
|
The generated random number is returned in the argument
|
|
<I>result</I>.
|
|
|
|
<P>
|
|
|
|
The
|
|
<B>srandom_r</B>()
|
|
|
|
function is like
|
|
<B><A HREF="/cgi-bin/man/man2html?3+srandom">srandom</A></B>(3),
|
|
|
|
except that it initializes the seed for the random number generator
|
|
whose state is maintained in the object pointed to by
|
|
<I>buf</I>,
|
|
|
|
which must have been previously initialized by
|
|
<B>initstate_r</B>(),
|
|
|
|
instead of the seed associated with the global state variable.
|
|
<P>
|
|
|
|
The
|
|
<B>initstate_r</B>()
|
|
|
|
function is like
|
|
<B><A HREF="/cgi-bin/man/man2html?3+initstate">initstate</A></B>(3)
|
|
|
|
except that it initializes the state in the object pointed to by
|
|
<I>buf</I>,
|
|
|
|
rather than initializing the global state variable.
|
|
Before calling this function, the
|
|
<I>buf.state</I>
|
|
|
|
field must be initialized to NULL.
|
|
The
|
|
<B>initstate_r</B>()
|
|
|
|
function records a pointer to the
|
|
<I>statebuf</I>
|
|
|
|
argument inside the structure pointed to by
|
|
<I>buf</I>.
|
|
|
|
Thus,
|
|
<I>statebuf</I>
|
|
|
|
should not be deallocated so long as
|
|
<I>buf</I>
|
|
|
|
is still in use.
|
|
(So,
|
|
<I>statebuf</I>
|
|
|
|
should typically be allocated as a static variable,
|
|
or allocated on the heap using
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3)
|
|
|
|
or similar.)
|
|
<P>
|
|
|
|
The
|
|
<B>setstate_r</B>()
|
|
|
|
function is like
|
|
<B><A HREF="/cgi-bin/man/man2html?3+setstate">setstate</A></B>(3)
|
|
|
|
except that it modifies the state in the object pointed to by
|
|
<I>buf</I>,
|
|
|
|
rather than modifying the global state variable.
|
|
<I>state</I> must first have been initialized
|
|
using
|
|
<B>initstate_r</B>()
|
|
|
|
or be the result of a previous call of
|
|
<B>setstate_r</B>().
|
|
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
All of these functions return 0 on success.
|
|
On error, -1 is returned, 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>
|
|
A state array of less than 8 bytes was specified to
|
|
<B>initstate_r</B>().
|
|
|
|
<DT id="3"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
The
|
|
<I>statebuf</I>
|
|
|
|
or
|
|
<I>buf</I>
|
|
|
|
argument to
|
|
<B>setstate_r</B>()
|
|
|
|
was NULL.
|
|
<DT id="4"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
The
|
|
<I>buf</I>
|
|
|
|
or
|
|
<I>result</I>
|
|
|
|
argument to
|
|
<B>random_r</B>()
|
|
|
|
was NULL.
|
|
</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_r</B>(),
|
|
|
|
<B>srandom_r</B>(),
|
|
|
|
<BR>
|
|
|
|
<B>initstate_r</B>(),
|
|
|
|
<B>setstate_r</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe race:buf<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<A NAME="lbAH"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
These functions are nonstandard glibc extensions.
|
|
|
|
|
|
<A NAME="lbAI"> </A>
|
|
<H2>BUGS</H2>
|
|
|
|
The
|
|
<B>initstate_r</B>()
|
|
|
|
interface is confusing.
|
|
|
|
It appears that the
|
|
<I>random_data</I>
|
|
|
|
type is intended to be opaque,
|
|
but the implementation requires the user to either initialize the
|
|
<I>buf.state</I>
|
|
|
|
field to NULL or zero out the entire structure before the call.
|
|
<A NAME="lbAJ"> </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+rand">rand</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+random">random</A></B>(3)
|
|
|
|
<A NAME="lbAK"> </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="5"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="6"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="7"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="8"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="9"><A HREF="#lbAF">ERRORS</A><DD>
|
|
<DT id="10"><A HREF="#lbAG">ATTRIBUTES</A><DD>
|
|
<DT id="11"><A HREF="#lbAH">CONFORMING TO</A><DD>
|
|
<DT id="12"><A HREF="#lbAI">BUGS</A><DD>
|
|
<DT id="13"><A HREF="#lbAJ">SEE ALSO</A><DD>
|
|
<DT id="14"><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:54 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|