1 .\" Copyright 2008 Michael Kerrisk <mtk.manpages@gmail.com>
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.
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.
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
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH RANDOM_R 3 2017-09-15 "GNU" "Linux Programmer's Manual"
28 random_r, srandom_r, initstate_r, setstate_r \- reentrant
29 random number generator
32 .B #include <stdlib.h>
34 .BI "int random_r(struct random_data *" buf ", int32_t *" result );
36 .BI "int srandom_r(unsigned int " seed ", struct random_data *" buf );
38 .BI "int initstate_r(unsigned int " seed ", char *" statebuf ,
39 .BI " size_t " statelen ", struct random_data *" buf );
41 .BI "int setstate_r(char *" statebuf ", struct random_data *" buf );
45 Feature Test Macro Requirements for glibc (see
46 .BR feature_test_macros (7)):
55 /* Glibc since 2.19: */ _DEFAULT_SOURCE
56 || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
60 These functions are the reentrant equivalents
61 of the functions described in
63 They are suitable for use in multithreaded programs where each thread
64 needs to obtain an independent, reproducible sequence of random numbers.
70 except that instead of using state information maintained
72 it uses the state information in the argument pointed to by
74 which must have been previously initialized by
76 The generated random number is returned in the argument
83 except that it initializes the seed for the random number generator
84 whose state is maintained in the object pointed to by
86 which must have been previously initialized by
88 instead of the seed associated with the global state variable.
94 except that it initializes the state in the object pointed to by
96 rather than initializing the global state variable.
97 Before calling this function, the
99 field must be initialized to NULL.
102 function records a pointer to the
104 argument inside the structure pointed to by
108 should not be deallocated so long as
113 should typically be allocated as a static variable,
114 or allocated on the heap using
122 except that it modifies the state in the object pointed to by
124 rather than modifying the global state variable.
125 \fIstate\fP must first have been initialized
128 or be the result of a previous call of
131 All of these functions return 0 on success.
132 On error, \-1 is returned, with
134 set to indicate the cause of the error.
138 A state array of less than 8 bytes was specified to
159 For an explanation of the terms used in this section, see
165 Interface Attribute Value
172 T} Thread safety MT-Safe race:buf
175 These functions are nonstandard glibc extensions.
176 .\" These functions appear to be on Tru64, but don't seem to be on
177 .\" Solaris, HP-UX, or FreeBSD.
181 interface is confusing.
182 .\" FIXME . https://sourceware.org/bugzilla/show_bug.cgi?id=3662
185 type is intended to be opaque,
186 but the implementation requires the user to either initialize the
188 field to NULL or zero out the entire structure before the call.