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 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(): _SVID_SOURCE ||
21       _BSD_SOURCE || _XOPEN_SOURCE >= 500
22

DESCRIPTION

24       The random() function uses a non-linear additive feedback random number
25       generator  employing a default table of size 31 long integers to return
26       successive pseudo-random numbers in the range from 0 to RAND_MAX.   The
27       period  of  this  random  number generator is very large, approximately
28       16 * ((2^31) - 1).
29
30       The srandom() function sets its argument as the seed for a new sequence
31       of  pseudo-random integers to be returned by random().  These sequences
32       are repeatable by calling srandom() with the same seed  value.   If  no
33       seed  value  is provided, the random() function is automatically seeded
34       with a value of 1.
35
36       The initstate() function allows a state array state to  be  initialized
37       for  use  by  random().  The size of the state array n is used by init‐
38       state() to decide how sophisticated a random number generator it should
39       use  —  the  larger the state array, the better the random numbers will
40       be.  seed is the seed for the initialization, which specifies a  start‐
41       ing  point  for the random number sequence, and provides for restarting
42       at the same point.
43
44       The setstate() function changes the state array used  by  the  random()
45       function.   The  state array state is used for random number generation
46       until the next call to initstate() or  setstate().   state  must  first
47       have  been initialized using initstate() or be the result of a previous
48       call of setstate().
49

RETURN VALUE

51       The random() function returns a value  between  0  and  RAND_MAX.   The
52       srandom()  function  returns  no value.  The initstate() and setstate()
53       functions return a pointer to the previous  state  array,  or  NULL  on
54       error.
55

ERRORS

57       EINVAL A state array of less than 8 bytes was specified to initstate().
58

CONFORMING TO

60       4.3BSD, POSIX.1-2001.
61

NOTES

63       Current  "optimal"  values for the size of the state array n are 8, 32,
64       64, 128, and 256 bytes; other amounts will be rounded down to the near‐
65       est known amount.  Using less than 8 bytes will cause an error.
66
67       This  function  should  not be used in cases where multiple threads use
68       random() and the behavior should be reproducible.  Use random_r(3)  for
69       that purpose.
70
71       Random-number  generation  is a complex topic.  Numerical Recipes in C:
72       The Art of Scientific Computing (William H. Press, Brian  P.  Flannery,
73       Saul  A.  Teukolsky, William T. Vetterling; New York: Cambridge Univer‐
74       sity Press, 2007, 3rd ed.)  provides an excellent discussion of practi‐
75       cal random-number generation issues in Chapter 7 (Random Numbers).
76
77       For  a  more  theoretical  discussion  which also covers many practical
78       issues in depth, see Chapter 3 (Random Numbers) in  Donald  E.  Knuth's
79       The  Art  of Computer Programming, volume 2 (Seminumerical Algorithms),
80       2nd ed.; Reading,  Massachusetts:  Addison-Wesley  Publishing  Company,
81       1981.
82

SEE ALSO

84       drand48(3), rand(3), random_r(3), srand(3)
85

COLOPHON

87       This  page  is  part of release 3.22 of the Linux man-pages project.  A
88       description of the project, and information about reporting  bugs,  can
89       be found at http://www.kernel.org/doc/man-pages/.
90
91
92
93GNU                               2009-02-03                         RANDOM(3)
Impressum