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

NAME

6       random, srandom, initstate, setstate - random number generator
7

SYNOPSIS

9       #include <stdlib.h>
10
11       long random(void);
12
13       void srandom(unsigned seed);
14
15       char *initstate(unsigned seed, char *state, size_t n);
16
17       char *setstate(char *state);
18
19   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
20
21       random(), srandom(), initstate(), setstate():
22           _XOPEN_SOURCE >= 500
23               || /* Glibc since 2.19: */ _DEFAULT_SOURCE
24               || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
25

DESCRIPTION

27       The  random() function uses a nonlinear additive feedback random number
28       generator employing a default table of size 31 long integers to  return
29       successive  pseudo-random numbers in the range from 0 to 2^31 - 1.  The
30       period of this random number generator  is  very  large,  approximately
31       16 * ((2^31) - 1).
32
33       The srandom() function sets its argument as the seed for a new sequence
34       of pseudo-random integers to be returned by random().  These  sequences
35       are  repeatable  by  calling srandom() with the same seed value.  If no
36       seed value is provided, the random() function is  automatically  seeded
37       with a value of 1.
38
39       The  initstate()  function allows a state array state to be initialized
40       for use by random().  The size of the state array n is  used  by  init‐
41       state() to decide how sophisticated a random number generator it should
42       use—the larger the state array, the better the random numbers will  be.
43       Current  "optimal"  values for the size of the state array n are 8, 32,
44       64, 128, and 256 bytes; other amounts will be rounded down to the near‐
45       est  known  amount.  Using less than 8 bytes results in an error.  seed
46       is the seed for the initialization, which specifies  a  starting  point
47       for the random number sequence, and provides for restarting at the same
48       point.
49
50       The setstate() function changes the state array used  by  the  random()
51       function.   The  state array state is used for random number generation
52       until the next call to initstate() or  setstate().   state  must  first
53       have  been initialized using initstate() or be the result of a previous
54       call of setstate().
55

RETURN VALUE

57       The random() function returns a value between 0  and  (2^31) - 1.   The
58       srandom() function returns no value.
59
60       The initstate() function returns a pointer to the previous state array.
61       On error, errno is set to indicate the cause.
62
63       On success, setstate() returns a pointer to the previous  state  array.
64       On  error, it returns NULL, with errno set to indicate the cause of the
65       error.
66

ERRORS

68       EINVAL The state argument given to setstate() was NULL.
69
70       EINVAL A state array of less than 8 bytes was specified to initstate().
71

ATTRIBUTES

73       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
74       tributes(7).
75
76       ┌────────────────────────┬───────────────┬─────────┐
77Interface               Attribute     Value   
78       ├────────────────────────┼───────────────┼─────────┤
79random(), srandom(),    │ Thread safety │ MT-Safe │
80initstate(), setstate() │               │         │
81       └────────────────────────┴───────────────┴─────────┘

CONFORMING TO

83       POSIX.1-2001, POSIX.1-2008, 4.3BSD.
84

NOTES

86       The  random()  function  should  not  be used in multithreaded programs
87       where reproducible behavior is required.  Use random_r(3) for that pur‐
88       pose.
89
90       Random-number  generation  is a complex topic.  Numerical Recipes in C:
91       The Art of Scientific Computing (William H. Press, Brian  P.  Flannery,
92       Saul  A.  Teukolsky, William T. Vetterling; New York: Cambridge Univer‐
93       sity Press, 2007, 3rd ed.)  provides an excellent discussion of practi‐
94       cal random-number generation issues in Chapter 7 (Random Numbers).
95
96       For  a more theoretical discussion which also covers many practical is‐
97       sues in depth, see Chapter 3 (Random Numbers) in Donald E. Knuth's  The
98       Art  of  Computer Programming, volume 2 (Seminumerical Algorithms), 2nd
99       ed.; Reading, Massachusetts: Addison-Wesley Publishing Company, 1981.
100

BUGS

102       According to POSIX, initstate() should return NULL on  error.   In  the
103       glibc  implementation,  errno  is  (as specified) set on error, but the
104       function does not return NULL.
105

SEE ALSO

107       getrandom(2), drand48(3), rand(3), random_r(3), srand(3)
108

COLOPHON

110       This page is part of release 5.10 of the Linux  man-pages  project.   A
111       description  of  the project, information about reporting bugs, and the
112       latest    version    of    this    page,    can     be     found     at
113       https://www.kernel.org/doc/man-pages/.
114
115
116
117GNU                               2020-11-01                         RANDOM(3)
Impressum