random_r(3)
π random_r(3) β the reentrant random number generator girlies π
bestie. come sit. we're talking random_r(3) today, from the Library Functions Manual section 3, and honestly? it's giving "the popular one but with trust issues" because these are the thread-safe versions. obsessed.
β¨ NAME β¨
random_r, srandom_r, initstate_r, setstate_r β the whole squad: reentrant random number generator functions. no cap.
π LIBRARY π
Standard C library (libc, -lc). yeah. the classic. π
π SYNOPSIS π
okay so first, the include, obviously:
#include <stdlib.h>
and here come the signatures, copy them EXACTLY, i'm not playing:
int random_r(struct random_data *restrict buf,
int32_t *restrict result);
int srandom_r(unsigned int seed, struct random_data *buf);
int initstate_r(unsigned int seed,
char statebuf[restrict .statelen], size_t statelen,
struct random_data *restrict buf);
int setstate_r(char *restrict statebuf,
struct random_data *restrict buf);
π Feature Test Macro Requirements for glibc (peep feature_test_macros(7) for the lore) π
random_r(), srandom_r(), initstate_r(), setstate_r():
/* glibc >= 2.19: */ _DEFAULT_SOURCE
|| /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
so like. if you're on ancient glibc you gotta define _SVID_SOURCE or _BSD_SOURCE. _DEFAULT_SOURCE era girlies are chillin. π¦
πΈ DESCRIPTION πΈ
so these functions are literally the reentrant equivalents of the ones described over in random(3). meaning? they're perfect for multithreaded programs where every single thread needs its own independent, reproducible sequence of random numbers. no fighting over global state, no drama, main character energy for each thread fr.
random_r() is giving random(3) but make it β¨selfishβ¨ β instead of using state info kept in a global variable, it uses the state info in the argument pointed to by buf, which must have been previously initialized by initstate_r(). the generated random number gets handed back to you in the argument result. π
srandom_r() is like srandom(3), except it initializes the seed for the random number generator whose state lives in the object pointed to by buf β which, yes bestie, must have been previously initialized by initstate_r() β instead of the seed associated with the global state variable. she's independent. she doesn't share. π
initstate_r() is like initstate(3), except it initializes the state in the object pointed to by buf, rather than initializing the global state variable. β οΈ before calling this function, the buf.state field must be initialized to NULL. also β and this is the important tea π« β initstate_r() records a pointer to the statebuf argument inside the structure pointed to by buf. so like, statebuf should not be deallocated so long as buf is still in use. (which means: statebuf should typically be allocated as a static variable, or allocated on the heap using malloc(3) or similar.) don't free it early i'm begging π
setstate_r() is like setstate(3), except it modifies the state in the object pointed to by buf, rather than modifying the global state variable. state must first have been initialized using initstate_r() or be the result of a previous call of setstate_r(). periodt. π
β RETURN VALUE β
all of these functions return 0 on success. on error, -1 is returned, with errno set to indicate the error. yes ma'am, classic. π
π ERRORS π
EINVALβ a state array of less than 8 bytes was specified toinitstate_r(). too small bestie, she needs at least 8 πEINVALβ thestatebuforbufargument tosetstate_r()wasNULL. you cannot passNULL, i will find you πEINVALβ thebuforresultargument torandom_r()wasNULL. same energy. stop it. π
π₯ ATTRIBUTES π₯
for an explanation of the terms used in this section, see attributes(7). here's the vibe check, table-ified into bullets because we're cute like that:
- Interface:
random_r(),srandom_r(),initstate_r(),setstate_r() - Attribute: Thread safety
- Value:
MT-Safe race:buf
so they're MT-Safe but with a race on buf β meaning: don't go sharing one buf across threads without locking, iykyk. β¨
π§ STANDARDS π§
GNU. yeah, she's a GNU girlie. that's the whole answer. π
π BUGS π
bestie, listen. the initstate_r() interface is confusing. it appears that the random_data type is intended to be opaque, but the implementation requires the user to either initialize the buf.state field to NULL or zero out the entire structure before the call. like. pick a lane. π it's a documented bug note, not me being dramatic β that's literally what it says.
π«Ά SEE ALSO π«Ά
drand48(3), rand(3), random(3) β go read those too, bestie. knowledge is free and it's slay. πΈ
and that's the page!! π this was from Linux man-pages 6.20, dated 2026-02-08, random_r(3) β stan a thread-safe random generator, stay safe out there with your buf, and don't forget to init that state field to NULL or i'm gonna cry ππ bye bestieeeee π¦β¨
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- Copyright 2008, Michael Kerrisk <mtk.manpages@gmail.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms