]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/random_r.3
mdoc.7: wfix
[thirdparty/man-pages.git] / 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 2016-03-15 "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 /* Glibc since 2.19: */ _DEFAULT_SOURCE
56 || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
57 .RE
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.
65
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
73 .IR buf ,
74 which must have been previously initialized by
75 .BR initstate_r ().
76 The generated random number is returned in the argument
77 .IR result .
78
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 ,
86 which must have been previously initialized by
87 .BR initstate_r (),
88 instead of the seed associated with the global state variable.
89
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.
97
98 The
99 .BR setstate_r ()
100 function is like
101 .BR setstate (3)
102 except that it modifies the state in the object pointer to by
103 .IR buf ,
104 rather than modifying the global state variable.
105 .SH RETURN VALUE
106 All of these functions return 0 on success.
107 On error, \-1 is returned, with
108 .I errno
109 set to indicate the cause of the error.
110 .SH ERRORS
111 .TP
112 .B EINVAL
113 A state array of less than 8 bytes was specified to
114 .BR initstate_r ().
115 .TP
116 .B EINVAL
117 The
118 .I statebuf
119 or
120 .I buf
121 argument to
122 .BR setstate_r ()
123 was NULL.
124 .TP
125 .B EINVAL
126 The
127 .I buf
128 or
129 .I result
130 argument to
131 .BR random_r ()
132 was NULL.
133 .SH ATTRIBUTES
134 For an explanation of the terms used in this section, see
135 .BR attributes (7).
136 .TS
137 allbox;
138 lbw27 lb lb
139 l l l.
140 Interface Attribute Value
141 T{
142 .BR random_r (),
143 .BR srandom_r (),
144 .br
145 .BR initstate_r (),
146 .BR setstate_r ()
147 T} Thread safety MT-Safe race:buf
148 .TE
149 .SH CONFORMING TO
150 These functions are nonstandard glibc extensions.
151 .\" These functions appear to be on Tru64, but don't seem to be on
152 .\" Solaris, HP-UX, or FreeBSD.
153 .SH SEE ALSO
154 .BR drand48 (3),
155 .BR rand (3),
156 .BR random (3)