1INITSTATE(3P) POSIX Programmer's Manual INITSTATE(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 initstate, random, setstate, srandom — pseudo-random number functions
13
15 #include <stdlib.h>
16
17 char *initstate(unsigned seed, char *state, size_t size);
18 long random(void);
19 char *setstate(char *state);
20 void srandom(unsigned seed);
21
23 The random() function shall use a non-linear additive feedback random-
24 number generator employing a default state array size of 31 long inte‐
25 gers to return successive pseudo-random numbers in the range from 0 to
26 231-1. The period of this random-number generator is approximately 16 x
27 (231-1). The size of the state array determines the period of the ran‐
28 dom-number generator. Increasing the state array size shall increase
29 the period.
30
31 With 256 bytes of state information, the period of the random-number
32 generator shall be greater than 269.
33
34 Like rand(), random() shall produce by default a sequence of numbers
35 that can be duplicated by calling srandom() with 1 as the seed.
36
37 The srandom() function shall initialize the current state array using
38 the value of seed.
39
40 The initstate() and setstate() functions handle restarting and changing
41 random-number generators. The initstate() function allows a state
42 array, pointed to by the state argument, to be initialized for future
43 use. The size argument, which specifies the size in bytes of the state
44 array, shall be used by initstate() to decide what type of random-num‐
45 ber generator to use; the larger the state array, the more random the
46 numbers. Values for the amount of state information are 8, 32, 64, 128,
47 and 256 bytes. Other values greater than 8 bytes are rounded down to
48 the nearest one of these values. If initstate() is called with
49 8≤size<32, then random() shall use a simple linear congruential random
50 number generator. The seed argument specifies a starting point for the
51 random-number sequence and provides for restarting at the same point.
52 The initstate() function shall return a pointer to the previous state
53 information array.
54
55 If initstate() has not been called, then random() shall behave as
56 though initstate() had been called with seed=1 and size=128.
57
58 Once a state has been initialized, setstate() allows switching between
59 state arrays. The array defined by the state argument shall be used for
60 further random-number generation until initstate() is called or set‐
61 state() is called again. The setstate() function shall return a pointer
62 to the previous state array.
63
65 If initstate() is called with size less than 8, it shall return NULL.
66
67 The random() function shall return the generated pseudo-random number.
68
69 The srandom() function shall not return a value.
70
71 Upon successful completion, initstate() and setstate() shall return a
72 pointer to the previous state array; otherwise, a null pointer shall be
73 returned.
74
76 No errors are defined.
77
78 The following sections are informative.
79
81 None.
82
84 After initialization, a state array can be restarted at a different
85 point in one of two ways:
86
87 1. The initstate() function can be used, with the desired seed, state
88 array, and size of the array.
89
90 2. The setstate() function, with the desired state, can be used, fol‐
91 lowed by srandom() with the desired seed. The advantage of using
92 both of these functions is that the size of the state array does
93 not have to be saved once it is initialized.
94
95 Although some implementations of random() have written messages to
96 standard error, such implementations do not conform to POSIX.1‐2008.
97
98 Issue 5 restored the historical behavior of this function.
99
100 Threaded applications should use erand48(), nrand48(), or jrand48()
101 instead of random() when an independent random number sequence in mul‐
102 tiple threads is required.
103
104 These functions should be avoided whenever non-trivial requirements
105 (including safety) have to be fulfilled.
106
108 None.
109
111 None.
112
114 drand48(), rand()
115
116 The Base Definitions volume of POSIX.1‐2017, <stdlib.h>
117
119 Portions of this text are reprinted and reproduced in electronic form
120 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
121 table Operating System Interface (POSIX), The Open Group Base Specifi‐
122 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
123 Electrical and Electronics Engineers, Inc and The Open Group. In the
124 event of any discrepancy between this version and the original IEEE and
125 The Open Group Standard, the original IEEE and The Open Group Standard
126 is the referee document. The original Standard can be obtained online
127 at http://www.opengroup.org/unix/online.html .
128
129 Any typographical or formatting errors that appear in this page are
130 most likely to have been introduced during the conversion of the source
131 files to man page format. To report such errors, see https://www.ker‐
132 nel.org/doc/man-pages/reporting_bugs.html .
133
134
135
136IEEE/The Open Group 2017 INITSTATE(3P)