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 char *setstate(char *state);
17
18 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
19
20 random(), srandom(), initstate(), setstate():
21 _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
22 _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
23
25 The random() function uses a nonlinear additive feedback random number
26 generator employing a default table of size 31 long integers to return
27 successive pseudo-random numbers in the range from 0 to RAND_MAX. The
28 period of this random number generator is very large, approximately
29 16 * ((2^31) - 1).
30
31 The srandom() function sets its argument as the seed for a new sequence
32 of pseudo-random integers to be returned by random(). These sequences
33 are repeatable by calling srandom() with the same seed value. If no
34 seed value is provided, the random() function is automatically seeded
35 with a value of 1.
36
37 The initstate() function allows a state array state to be initialized
38 for use by random(). The size of the state array n is used by init‐
39 state() to decide how sophisticated a random number generator it should
40 use—the larger the state array, the better the random numbers will be.
41 seed is the seed for the initialization, which specifies a starting
42 point for the random number sequence, and provides for restarting at
43 the same point.
44
45 The setstate() function changes the state array used by the random()
46 function. The state array state is used for random number generation
47 until the next call to initstate() or setstate(). state must first
48 have been initialized using initstate() or be the result of a previous
49 call of setstate().
50
52 The random() function returns a value between 0 and RAND_MAX. The
53 srandom() function returns no value.
54
55 The initstate() function returns a pointer to the previous state array.
56 On error, errno is set to indicate the cause.
57
58 On success, setstate() returns a pointer to the previous state array.
59 On error, it returns NULL, with errno set to indicate the cause of the
60 error.
61
63 EINVAL The state argument given to setstate() was NULL.
64
65 EINVAL A state array of less than 8 bytes was specified to initstate().
66
68 4.3BSD, POSIX.1-2001.
69
71 Current "optimal" values for the size of the state array n are 8, 32,
72 64, 128, and 256 bytes; other amounts will be rounded down to the near‐
73 est known amount. Using less than 8 bytes will cause an error.
74
75 This function should not be used in cases where multiple threads use
76 random() and the behavior should be reproducible. Use random_r(3) for
77 that purpose.
78
79 Random-number generation is a complex topic. Numerical Recipes in C:
80 The Art of Scientific Computing (William H. Press, Brian P. Flannery,
81 Saul A. Teukolsky, William T. Vetterling; New York: Cambridge Univer‐
82 sity Press, 2007, 3rd ed.) provides an excellent discussion of practi‐
83 cal random-number generation issues in Chapter 7 (Random Numbers).
84
85 For a more theoretical discussion which also covers many practical
86 issues in depth, see Chapter 3 (Random Numbers) in Donald E. Knuth's
87 The Art of Computer Programming, volume 2 (Seminumerical Algorithms),
88 2nd ed.; Reading, Massachusetts: Addison-Wesley Publishing Company,
89 1981.
90
92 According to POSIX, initstate() should return NULL on error. In the
93 glibc implementation, errno is (as specified) set on error, but the
94 function does not return NULL.
95
97 drand48(3), rand(3), random_r(3), srand(3)
98
100 This page is part of release 3.53 of the Linux man-pages project. A
101 description of the project, and information about reporting bugs, can
102 be found at http://www.kernel.org/doc/man-pages/.
103
104
105
106GNU 2013-04-19 RANDOM(3)