NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |ATTRIBUTES |STANDARDS |BUGS |SEE ALSO |COLOPHON | |
random_r(3) Library Functions Manualrandom_r(3)random_r, srandom_r, initstate_r, setstate_r - reentrant random number generator
Standard C library (libc,-lc)
#include <stdlib.h>int random_r(struct random_data *restrictbuf,int32_t *restrictresult);int srandom_r(unsigned intseed, struct random_data *buf);int initstate_r(unsigned intseed, charstatebuf[restrict .statelen],size_tstatelen, struct random_data *restrictbuf);int setstate_r(char *restrictstatebuf,struct random_data *restrictbuf); Feature Test Macro Requirements for glibc (seefeature_test_macros(7)):random_r(),srandom_r(),initstate_r(),setstate_r(): /* glibc >= 2.19: */ _DEFAULT_SOURCE || /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
These functions are the reentrant equivalents of the functions described inrandom(3). They are suitable for use in multithreaded programs where each thread needs to obtain an independent, reproducible sequence of random numbers. Therandom_r() function is likerandom(3), except that instead of using state information maintained in a global variable, it uses the state information in the argument pointed to bybuf, which must have been previously initialized byinitstate_r(). The generated random number is returned in the argumentresult. Thesrandom_r() function is likesrandom(3), except that it initializes the seed for the random number generator whose state is maintained in the object pointed to bybuf, which must have been previously initialized byinitstate_r(), instead of the seed associated with the global state variable. Theinitstate_r() function is likeinitstate(3) except that it initializes the state in the object pointed to bybuf, rather than initializing the global state variable. Before calling this function, thebuf.state field must be initialized to NULL. Theinitstate_r() function records a pointer to thestatebuf argument inside the structure pointed to bybuf. Thus,statebuf should not be deallocated so long asbuf is still in use. (So,statebuf should typically be allocated as a static variable, or allocated on the heap usingmalloc(3) or similar.) Thesetstate_r() function is likesetstate(3) except that it modifies the state in the object pointed to bybuf, rather than modifying the global state variable.state must first have been initialized usinginitstate_r() or be the result of a previous call ofsetstate_r().
All of these functions return 0 on success. On error, -1 is returned, witherrno set to indicate the error.
EINVALA state array of less than 8 bytes was specified toinitstate_r().EINVALThestatebuf orbuf argument tosetstate_r() was NULL.EINVALThebuf orresult argument torandom_r() was NULL.
For an explanation of the terms used in this section, seeattributes(7). ┌─────────────────────────────┬───────────────┬──────────────────┐ │Interface│Attribute│Value│ ├─────────────────────────────┼───────────────┼──────────────────┤ │random_r(),srandom_r(), │ Thread safety │ MT-Safe race:buf │ │initstate_r(),setstate_r() │ │ │ └─────────────────────────────┴───────────────┴──────────────────┘
GNU.
Theinitstate_r() interface is confusing. It appears that therandom_data type is intended to be opaque, but the implementation requires the user to either initialize thebuf.state field to NULL or zero out the entire structure before the call.
drand48(3),rand(3),random(3)
This page is part of theman-pages (Linux kernel and C library user-space interface documentation) project. Information about the project can be found at ⟨https://www.kernel.org/doc/man-pages/⟩. If you have a bug report for this manual page, see ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩. This page was obtained from the tarball man-pages-6.15.tar.gz fetched from ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on 2025-08-11. If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up- to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgLinux man-pages 6.15 2025-05-17random_r(3)Pages that refer to this page:random(3)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |