1 .\" Copyright 2008 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH random_r 3 (date) "Linux man-pages (unreleased)"
8 random_r, srandom_r, initstate_r, setstate_r \- reentrant
9 random number generator
12 .RI ( libc ", " \-lc )
15 .B #include <stdlib.h>
17 .BI "int random_r(struct random_data *restrict " buf ,
18 .BI " int32_t *restrict " result );
19 .BI "int srandom_r(unsigned int " seed ", struct random_data *" buf );
21 .BI "int initstate_r(unsigned int " seed ", char *restrict " statebuf ,
22 .BI " size_t " statelen ", struct random_data *restrict " buf );
23 .BI "int setstate_r(char *restrict " statebuf ,
24 .BI " struct random_data *restrict " buf );
28 Feature Test Macro Requirements for glibc (see
29 .BR feature_test_macros (7)):
37 /* Glibc since 2.19: */ _DEFAULT_SOURCE
38 || /* Glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
41 These functions are the reentrant equivalents
42 of the functions described in
44 They are suitable for use in multithreaded programs where each thread
45 needs to obtain an independent, reproducible sequence of random numbers.
51 except that instead of using state information maintained
53 it uses the state information in the argument pointed to by
55 which must have been previously initialized by
57 The generated random number is returned in the argument
64 except that it initializes the seed for the random number generator
65 whose state is maintained in the object pointed to by
67 which must have been previously initialized by
69 instead of the seed associated with the global state variable.
75 except that it initializes the state in the object pointed to by
77 rather than initializing the global state variable.
78 Before calling this function, the
80 field must be initialized to NULL.
83 function records a pointer to the
85 argument inside the structure pointed to by
89 should not be deallocated so long as
94 should typically be allocated as a static variable,
95 or allocated on the heap using
103 except that it modifies the state in the object pointed to by
105 rather than modifying the global state variable.
106 \fIstate\fP must first have been initialized
109 or be the result of a previous call of
112 All of these functions return 0 on success.
113 On error, \-1 is returned, with
115 set to indicate the error.
119 A state array of less than 8 bytes was specified to
140 For an explanation of the terms used in this section, see
148 Interface Attribute Value
154 T} Thread safety MT-Safe race:buf
160 These functions are nonstandard glibc extensions.
161 .\" These functions appear to be on Tru64, but don't seem to be on
162 .\" Solaris, HP-UX, or FreeBSD.
166 interface is confusing.
167 .\" FIXME . https://sourceware.org/bugzilla/show_bug.cgi?id=3662
170 type is intended to be opaque,
171 but the implementation requires the user to either initialize the
173 field to NULL or zero out the entire structure before the call.