1RANDOM_R(3)                Linux Programmer's Manual               RANDOM_R(3)
2
3
4

NAME

6       random_r,  srandom_r, initstate_r, setstate_r - reentrant random number
7       generator
8

SYNOPSIS

10       #include <stdlib.h>
11
12       int random_r(struct random_data *restrict buf,
13                    int32_t *restrict result);
14       int srandom_r(unsigned int seed, struct random_data *buf);
15
16       int initstate_r(unsigned int seed, char *restrict statebuf,
17                    size_t statelen, struct random_data *restrict buf);
18       int setstate_r(char *restrict statebuf,
19                    struct random_data *restrict buf);
20
21   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
22
23       random_r(), srandom_r(), initstate_r(), setstate_r():
24           /* Glibc since 2.19: */ _DEFAULT_SOURCE
25               || /* Glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
26

DESCRIPTION

28       These functions are the reentrant  equivalents  of  the  functions  de‐
29       scribed  in random(3).  They are suitable for use in multithreaded pro‐
30       grams where each thread needs to obtain  an  independent,  reproducible
31       sequence of random numbers.
32
33       The random_r() function is like random(3), except that instead of using
34       state information maintained in a global variable, it  uses  the  state
35       information  in  the  argument  pointed to by buf, which must have been
36       previously initialized by initstate_r().  The generated  random  number
37       is returned in the argument result.
38
39       The srandom_r() function is like srandom(3), except that it initializes
40       the seed for the random number generator whose state is  maintained  in
41       the  object pointed to by buf, which must have been previously initial‐
42       ized by initstate_r(), instead of the seed associated with  the  global
43       state variable.
44
45       The initstate_r() function is like initstate(3) except that it initial‐
46       izes the state in the object pointed to by buf, rather than  initializ‐
47       ing  the  global  state  variable.   Before  calling this function, the
48       buf.state field must be initialized to NULL.  The  initstate_r()  func‐
49       tion  records  a  pointer to the statebuf argument inside the structure
50       pointed to by buf.  Thus, statebuf should not be deallocated so long as
51       buf  is still in use.  (So, statebuf should typically be allocated as a
52       static variable, or allocated on the heap using malloc(3) or similar.)
53
54       The setstate_r() function is like setstate(3) except that  it  modifies
55       the  state  in  the object pointed to by buf, rather than modifying the
56       global state variable.  state must first have  been  initialized  using
57       initstate_r() or be the result of a previous call of setstate_r().
58

RETURN VALUE

60       All  of these functions return 0 on success.  On error, -1 is returned,
61       with errno set to indicate the error.
62

ERRORS

64       EINVAL A state array of less  than  8  bytes  was  specified  to  init‐
65              state_r().
66
67       EINVAL The statebuf or buf argument to setstate_r() was NULL.
68
69       EINVAL The buf or result argument to random_r() was NULL.
70

ATTRIBUTES

72       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
73       tributes(7).
74
75       ┌───────────────────────────────────┬───────────────┬──────────────────┐
76Interface                          Attribute     Value            
77       ├───────────────────────────────────┼───────────────┼──────────────────┤
78random_r(), srandom_r(),           │ Thread safety │ MT-Safe race:buf │
79initstate_r(), setstate_r()        │               │                  │
80       └───────────────────────────────────┴───────────────┴──────────────────┘
81

CONFORMING TO

83       These functions are nonstandard glibc extensions.
84

BUGS

86       The  initstate_r()  interface  is  confusing.  It appears that the ran‐
87       dom_data type is intended to be opaque, but the implementation requires
88       the  user  to either initialize the buf.state field to NULL or zero out
89       the entire structure before the call.
90

SEE ALSO

92       drand48(3), rand(3), random(3)
93

COLOPHON

95       This page is part of release 5.12 of the Linux  man-pages  project.   A
96       description  of  the project, information about reporting bugs, and the
97       latest    version    of    this    page,    can     be     found     at
98       https://www.kernel.org/doc/man-pages/.
99
100
101
102GNU                               2021-03-22                       RANDOM_R(3)
Impressum