OSDN Git Service

6089bcdb71e34f8ec6e096b7da3c013bd1a6eb0e
[linuxjm/LDP_man-pages.git] / original / man3 / random_r.3
1 .\" Copyright 2008 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\"
26 .TH RANDOM_R 3  2014-03-25 "GNU" "Linux Programmer's Manual"
27 .SH NAME
28 random_r, srandom_r, initstate_r, setstate_r \- reentrant
29 random number generator
30 .SH SYNOPSIS
31 .nf
32 .B #include <stdlib.h>
33 .sp
34 .BI "int random_r(struct random_data *" buf ", int32_t *" result );
35
36 .BI "int srandom_r(unsigned int " seed ", struct random_data *" buf );
37
38 .BI "int initstate_r(unsigned int " seed ", char *" statebuf ,
39 .BI "                size_t " statelen ", struct random_data *" buf );
40 .br
41 .BI "int setstate_r(char *" statebuf ", struct random_data *" buf );
42 .fi
43 .sp
44 .in -4n
45 Feature Test Macro Requirements for glibc (see
46 .BR feature_test_macros (7)):
47 .in
48 .sp
49 .ad l
50 .BR random_r (),
51 .BR srandom_r (),
52 .BR initstate_r (),
53 .BR setstate_r ():
54 .RS 4
55 _SVID_SOURCE || _BSD_SOURCE
56 .RE
57 .ad b
58 .SH DESCRIPTION
59 These functions are the reentrant equivalents
60 of the functions described in
61 .BR random (3).
62 They are suitable for use in multithreaded programs where each thread
63 needs to obtain an independent, reproducible sequence of random numbers.
64
65 The
66 .BR random_r ()
67 function is like
68 .BR random (3),
69 except that instead of using state information maintained
70 in a global variable,
71 it uses the state information in the argument pointed to by
72 .IR buf .
73 The generated random number is returned in the argument
74 .IR result .
75
76 The
77 .BR srandom_r ()
78 function is like
79 .BR srandom (3),
80 except that it initializes the seed for the random number generator
81 whose state is maintained in the object pointed to by
82 .IR buf ,
83 instead of the seed associated with the global state variable.
84
85 The
86 .BR initstate_r ()
87 function is like
88 .BR initstate (3)
89 except that it initializes the state in the object pointed to by
90 .IR buf ,
91 rather than initializing the global state variable.
92
93 The
94 .BR setstate_r ()
95 function is like
96 .BR setstate (3)
97 except that it modifies the state in the object pointer to by
98 .IR buf ,
99 rather than modifying the global state variable.
100 .SH RETURN VALUE
101 All of these functions return 0 on success.
102 On error, \-1 is returned, with
103 .I errno
104 set to indicate the cause of the error.
105 .SH ERRORS
106 .TP
107 .B EINVAL
108 A state array of less than 8 bytes was specified to
109 .BR initstate_r ().
110 .TP
111 .B EINVAL
112 The
113 .I statebuf
114 or
115 .I buf
116 argument to
117 .BR setstate_r ()
118 was NULL.
119 .TP
120 .B EINVAL
121 The
122 .I buf
123 or
124 .I result
125 argument to
126 .BR random_r ()
127 was NULL.
128 .SH ATTRIBUTES
129 .SS Multithreading (see pthreads(7))
130 The
131 .BR random_r (),
132 .BR srandom_r (),
133 .BR initstate_r (),
134 and
135 .BR setstate_r ()
136 functions are thread-safe.
137 .SH CONFORMING TO
138 These functions are nonstandard glibc extensions.
139 .\" These functions appear to be on Tru64, but don't seem to be on
140 .\" Solaris, HP-UX, or FreeBSD.
141 .SH SEE ALSO
142 .BR drand48 (3),
143 .BR rand (3),
144 .BR random (3)
145 .SH COLOPHON
146 This page is part of release 3.67 of the Linux
147 .I man-pages
148 project.
149 A description of the project,
150 information about reporting bugs,
151 and the latest version of this page,
152 can be found at
153 \%http://www.kernel.org/doc/man\-pages/.