]>
Commit | Line | Data |
---|---|---|
ceb141cf MK |
1 | .\" Copyright 2008 Michael Kerrisk <mtk.manpages@gmail.com> |
2 | .\" | |
93015253 | 3 | .\" %%%LICENSE_START(VERBATIM) |
ceb141cf MK |
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. | |
4b72fb64 | 23 | .\" %%%LICENSE_END |
ceb141cf MK |
24 | .\" |
25 | .\" | |
4b8c67d9 | 26 | .TH RANDOM_R 3 2017-09-15 "GNU" "Linux Programmer's Manual" |
ceb141cf | 27 | .SH NAME |
b75101c3 | 28 | random_r, srandom_r, initstate_r, setstate_r \- reentrant |
ceb141cf | 29 | random number generator |
ceb141cf MK |
30 | .SH SYNOPSIS |
31 | .nf | |
32 | .B #include <stdlib.h> | |
68e4db0a | 33 | .PP |
ceb141cf | 34 | .BI "int random_r(struct random_data *" buf ", int32_t *" result ); |
dbfe9c70 | 35 | .PP |
ceb141cf | 36 | .BI "int srandom_r(unsigned int " seed ", struct random_data *" buf ); |
dbfe9c70 | 37 | .PP |
ceb141cf MK |
38 | .BI "int initstate_r(unsigned int " seed ", char *" statebuf , |
39 | .BI " size_t " statelen ", struct random_data *" buf ); | |
dbfe9c70 | 40 | .PP |
ceb141cf MK |
41 | .BI "int setstate_r(char *" statebuf ", struct random_data *" buf ); |
42 | .fi | |
68e4db0a | 43 | .PP |
ceb141cf MK |
44 | .in -4n |
45 | Feature Test Macro Requirements for glibc (see | |
46 | .BR feature_test_macros (7)): | |
47 | .in | |
68e4db0a | 48 | .PP |
ceb141cf MK |
49 | .ad l |
50 | .BR random_r (), | |
51 | .BR srandom_r (), | |
52 | .BR initstate_r (), | |
53 | .BR setstate_r (): | |
2565afda | 54 | .RS 4 |
2b1b0424 MK |
55 | /* Glibc since 2.19: */ _DEFAULT_SOURCE |
56 | || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE | |
2565afda | 57 | .RE |
ceb141cf MK |
58 | .ad b |
59 | .SH DESCRIPTION | |
60 | These functions are the reentrant equivalents | |
61 | of the functions described in | |
62 | .BR random (3). | |
63 | They are suitable for use in multithreaded programs where each thread | |
64 | needs to obtain an independent, reproducible sequence of random numbers. | |
847e0d88 | 65 | .PP |
ceb141cf MK |
66 | The |
67 | .BR random_r () | |
68 | function is like | |
69 | .BR random (3), | |
70 | except that instead of using state information maintained | |
71 | in a global variable, | |
72 | it uses the state information in the argument pointed to by | |
61f6d1af MK |
73 | .IR buf , |
74 | which must have been previously initialized by | |
75 | .BR initstate_r (). | |
ceb141cf MK |
76 | The generated random number is returned in the argument |
77 | .IR result . | |
847e0d88 | 78 | .PP |
ceb141cf MK |
79 | The |
80 | .BR srandom_r () | |
81 | function is like | |
82 | .BR srandom (3), | |
83 | except that it initializes the seed for the random number generator | |
84 | whose state is maintained in the object pointed to by | |
85 | .IR buf , | |
61f6d1af MK |
86 | which must have been previously initialized by |
87 | .BR initstate_r (), | |
ceb141cf | 88 | instead of the seed associated with the global state variable. |
847e0d88 | 89 | .PP |
ceb141cf MK |
90 | The |
91 | .BR initstate_r () | |
92 | function is like | |
93 | .BR initstate (3) | |
94 | except that it initializes the state in the object pointed to by | |
95 | .IR buf , | |
96 | rather than initializing the global state variable. | |
91e37268 MK |
97 | Before calling this function, the |
98 | .IR buf.state | |
99 | field must be initialized to NULL. | |
516a3959 MK |
100 | The |
101 | .BR initstate_r () | |
102 | function records a pointer to the | |
103 | .I statebuf | |
104 | argument inside the structure pointed to by | |
105 | .IR buf . | |
106 | Thus, | |
107 | .IR statebuf | |
108 | should not be deallocated so long as | |
109 | .IR buf | |
110 | is still in use. | |
111 | (So, | |
112 | .I statebuf | |
113 | should typically be allocated as a static variable, | |
114 | or allocated on the heap using | |
115 | .BR malloc (3) | |
116 | or similar.) | |
847e0d88 | 117 | .PP |
ceb141cf MK |
118 | The |
119 | .BR setstate_r () | |
120 | function is like | |
121 | .BR setstate (3) | |
e00916fd | 122 | except that it modifies the state in the object pointed to by |
ceb141cf MK |
123 | .IR buf , |
124 | rather than modifying the global state variable. | |
4efbcb17 MK |
125 | \fIstate\fP must first have been initialized |
126 | using | |
127 | .BR initstate_r () | |
128 | or be the result of a previous call of | |
129 | .BR setstate_r (). | |
47297adb | 130 | .SH RETURN VALUE |
a824712b MK |
131 | All of these functions return 0 on success. |
132 | On error, \-1 is returned, with | |
133 | .I errno | |
134 | set to indicate the cause of the error. | |
ceb141cf MK |
135 | .SH ERRORS |
136 | .TP | |
137 | .B EINVAL | |
138 | A state array of less than 8 bytes was specified to | |
139 | .BR initstate_r (). | |
140 | .TP | |
141 | .B EINVAL | |
142 | The | |
143 | .I statebuf | |
144 | or | |
145 | .I buf | |
146 | argument to | |
147 | .BR setstate_r () | |
148 | was NULL. | |
149 | .TP | |
150 | .B EINVAL | |
151 | The | |
152 | .I buf | |
153 | or | |
154 | .I result | |
155 | argument to | |
156 | .BR random_r () | |
157 | was NULL. | |
53a70d91 | 158 | .SH ATTRIBUTES |
c0514f32 PH |
159 | For an explanation of the terms used in this section, see |
160 | .BR attributes (7). | |
161 | .TS | |
162 | allbox; | |
163 | lbw27 lb lb | |
164 | l l l. | |
165 | Interface Attribute Value | |
166 | T{ | |
53a70d91 PH |
167 | .BR random_r (), |
168 | .BR srandom_r (), | |
c0514f32 | 169 | .br |
53a70d91 | 170 | .BR initstate_r (), |
53a70d91 | 171 | .BR setstate_r () |
1b8bce69 | 172 | T} Thread safety MT-Safe race:buf |
c0514f32 | 173 | .TE |
47297adb | 174 | .SH CONFORMING TO |
c8f2dd47 | 175 | These functions are nonstandard glibc extensions. |
b75101c3 | 176 | .\" These functions appear to be on Tru64, but don't seem to be on |
ceb141cf | 177 | .\" Solaris, HP-UX, or FreeBSD. |
606b226b MK |
178 | .SH BUGS |
179 | The | |
180 | .BR initstate_r () | |
181 | interface is confusing. | |
182 | .\" FIXME . https://sourceware.org/bugzilla/show_bug.cgi?id=3662 | |
183 | It appears that the | |
184 | .IR random_data | |
185 | type is intended to be opaque, | |
186 | but the implementation requires the user to either initialize the | |
187 | .I buf.state | |
35ed4d7e | 188 | field to NULL or zero out the entire structure before the call. |
47297adb | 189 | .SH SEE ALSO |
ceb141cf MK |
190 | .BR drand48 (3), |
191 | .BR rand (3), | |
192 | .BR random (3) |