1RANDOM(3) Linux Programmer's Manual RANDOM(3)
2
3
4
6 random, srandom, initstate, setstate - random number generator
7
9 #include <stdlib.h>
10
11 long int random(void);
12
13 void srandom(unsigned int seed);
14
15 char *initstate(unsigned int 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
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
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
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
73 For an explanation of the terms used in this section, see
74 attributes(7).
75
76 ┌────────────────────────┬───────────────┬─────────┐
77 │Interface │ Attribute │ Value │
78 ├────────────────────────┼───────────────┼─────────┤
79 │random(), srandom(), │ Thread safety │ MT-Safe │
80 │initstate(), setstate() │ │ │
81 └────────────────────────┴───────────────┴─────────┘
83 POSIX.1-2001, POSIX.1-2008, 4.3BSD.
84
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
97 issues in depth, see Chapter 3 (Random Numbers) in Donald E. Knuth's
98 The Art of Computer Programming, volume 2 (Seminumerical Algorithms),
99 2nd ed.; Reading, Massachusetts: Addison-Wesley Publishing Company,
100 1981.
101
103 According to POSIX, initstate() should return NULL on error. In the
104 glibc implementation, errno is (as specified) set on error, but the
105 function does not return NULL.
106
108 getrandom(2), drand48(3), rand(3), random_r(3), srand(3)
109
111 This page is part of release 5.07 of the Linux man-pages project. A
112 description of the project, information about reporting bugs, and the
113 latest version of this page, can be found at
114 https://www.kernel.org/doc/man-pages/.
115
116
117
118GNU 2020-06-09 RANDOM(3)