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 RAND_MAX. 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 RAND_MAX. 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