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