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 *buf, int32_t *result);
13
14       int srandom_r(unsigned int seed, struct random_data *buf);
15
16       int initstate_r(unsigned int seed, char *statebuf,
17                       size_t statelen, struct random_data *buf);
18
19       int setstate_r(char *statebuf, struct random_data *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 versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
26

DESCRIPTION

28       These  functions  are  the  reentrant  equivalents  of  the   functions
29       described  in  random(3).   They  are suitable for use in multithreaded
30       programs 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 cause of 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
73       attributes(7).
74
75       ┌────────────────────────────┬───────────────┬──────────────────┐
76Interface                   Attribute     Value            
77       ├────────────────────────────┼───────────────┼──────────────────┤
78random_r(), srandom_r(),    │ Thread safety │ MT-Safe race:buf │
79initstate_r(), setstate_r() │               │                  │
80       └────────────────────────────┴───────────────┴──────────────────┘

CONFORMING TO

82       These functions are nonstandard glibc extensions.
83

BUGS

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

SEE ALSO

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

COLOPHON

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