summaryrefslogtreecommitdiff
path: root/usr/src/man/man3c/random.3c
blob: 8d1869a3437f9002f35c69ff1a447aae9704e173 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
.\"
.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
.\" permission to reproduce portions of its copyrighted documentation.
.\" Original documentation from The Open Group can be obtained online at
.\" http://www.opengroup.org/bookstore/.
.\"
.\" The Institute of Electrical and Electronics Engineers and The Open
.\" Group, have given us permission to reprint portions of their
.\" documentation.
.\"
.\" In the following statement, the phrase ``this text'' refers to portions
.\" of the system documentation.
.\"
.\" Portions of this text are reprinted and reproduced in electronic form
.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
.\" Standard for Information Technology -- Portable Operating System
.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
.\" between these versions and the original IEEE and The Open Group
.\" Standard, the original IEEE and The Open Group Standard is the referee
.\" document.  The original Standard can be obtained online at
.\" http://www.opengroup.org/unix/online.html.
.\"
.\" This notice shall appear on any product containing this material.
.\"
.\" The Berkeley software License Agreement specifies the terms and conditions
.\" for redistribution.
.\"
.\"
.\" Copyright (c) 1983 Regents of the University of California.
.\" All rights reserved.
.\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
.\" Copyright (c) 2002, Sun Microsystems, Inc.  All Rights Reserved.
.\"
.TH RANDOM 3C "Aug 14, 2002"
.SH NAME
random, srandom, initstate, setstate \- pseudorandom number functions
.SH SYNOPSIS
.LP
.nf
#include <stdlib.h>

\fBlong\fR \fBrandom\fR(\fBvoid\fR);
.fi

.LP
.nf
\fBvoid\fR \fBsrandom\fR(\fBunsigned int\fR \fIseed\fR);
.fi

.LP
.nf
\fBchar *\fR\fBinitstate\fR(\fBunsigned int\fR \fIseed\fR, \fBchar\fR \fI*state\fR, \fBsize_t\fR \fIsize\fR);
.fi

.LP
.nf
\fBchar *\fR\fBsetstate\fR(\fBconst char *\fR\fIstate\fR);
.fi

.SH DESCRIPTION
.LP
The \fBrandom()\fR function uses a nonlinear additive feedback random-number
generator employing a default state array size of 31 long integers to return
successive pseudo-random numbers in the range from 0 to 2^31 \(mi1. The period
of this random-number generator is approximately 16 x (2^31 \(mi1). The size of
the state array determines the period of the random-number generator.
Increasing the state array size increases the period.
.sp
.LP
The \fBsrandom()\fR function initializes the current state array using the
value of \fIseed\fR.
.sp
.LP
The \fBrandom()\fR and \fBsrandom()\fR functions have (almost) the same calling
sequence and initialization properties as \fBrand()\fR and \fBsrand()\fR (see
\fBrand\fR(3C)). The difference is that \fBrand\fR(3C) produces a much less
random sequence\(emin fact, the low dozen bits generated by rand go through a
cyclic pattern. All the bits generated by \fBrandom()\fR are usable.
.sp
.LP
The algorithm from \fBrand()\fR is used by \fBsrandom()\fR to generate the 31
state integers. Because of this, different \fBsrandom()\fR seeds often produce,
within an offset, the same sequence of low order bits from \fBrandom()\fR. If
low order bits are used directly, \fBrandom()\fR should be initialized with
\fBsetstate()\fR using high quality random values.
.sp
.LP
Unlike \fBsrand()\fR, \fBsrandom()\fR does not return the old seed because the
amount of state information used is much more than a single word. Two other
routines are provided to deal with restarting/changing random number
generators. With 256 bytes of state information, the period of the
random-number generator is greater than 2^69, which should be sufficient for
most purposes.
.sp
.LP
Like \fBrand\fR(3C), \fBrandom()\fR produces by default a sequence of numbers
that can be duplicated by calling \fBsrandom()\fR with 1 as the seed.
.sp
.LP
The \fBinitstate()\fR and \fBsetstate()\fR functions handle restarting and
changing random-number generators.  The \fBinitstate()\fR function allows a
state array, pointed to by the \fIstate\fR argument, to be initialized for
future use. The \fBsize\fR argument, which specifies the size in bytes of the
state array, is used by \fBinitstate()\fR to decide what type of random-number
generator to use; the larger the state array, the more random the numbers.
Values for the amount of state information are 8, 32, 64, 128, and 256 bytes.
Other values greater than 8 bytes are rounded down to the nearest one of these
values.  For values smaller than 8, \fBrandom()\fR uses a simple linear
congruential random number generator.  The \fIseed\fR argument specifies a
starting point for the random-number sequence and provides for restarting at
the same point.  The \fBinitstate()\fR function returns a pointer to the
previous state information array.
.sp
.LP
If \fBinitstate()\fR has not been called, then \fBrandom()\fR behaves as though
\fBinitstate()\fR had been called with \fIseed\fR\|=\|1 and \fIsize\fR\|=\|128.
.sp
.LP
If \fBinitstate()\fR is called with \fIsize\fR\|<\|8, then \fBrandom()\fR uses
a simple linear congruential random number generator.
.sp
.LP
Once a state has been initialized, \fBsetstate()\fR allows switching between
state arrays. The array defined by the \fIstate\fR argument is used for further
random-number generation until \fBinitstate()\fR is called or \fBsetstate()\fR
is called again. The \fBsetstate()\fR function returns a pointer to the
previous state array.
.sp
.LP
For a more powerful random number generator, see \fBarc4random\fR(3C).
.SH RETURN VALUES
.LP
The \fBrandom()\fR function returns the generated pseudo-random number.
.sp
.LP
The \fBsrandom()\fR function returns no value.
.sp
.LP
Upon successful completion, \fBinitstate()\fR and \fBsetstate()\fR return a
pointer to the previous state array.  Otherwise, a null pointer is returned.
.SH ERRORS
.LP
No errors are defined.
.SH USAGE
.LP
After initialization, a state array can be restarted at a different point in
one of two ways:
.RS +4
.TP
.ie t \(bu
.el o
The \fBinitstate()\fR function can be used, with the desired seed, state array,
and size of the array.
.RE
.RS +4
.TP
.ie t \(bu
.el o
The \fBsetstate()\fR function, with the desired state, can be used, followed by
\fBsrandom()\fR with the desired seed. The advantage of using both of these
functions is that the size of the state array does not have to be saved once it
is initialized.
.RE
.SH EXAMPLES
.LP
\fBExample 1 \fRInitialize an array.
.sp
.LP
The following example demonstrates the use of \fBinitstate()\fR to intialize an
array. It also demonstrates how to initialize an array and pass it to
\fBsetstate()\fR.

.sp
.in +2
.nf
# include <stdlib.h>
static unsigned int state0[32];
static unsigned int state1[32] = {
     3,
     0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342,
     0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd,
     0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86,
     0xda672e2a, 0x1588ca88, 0xe369735d, 0x904f35f7,
     0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc,
     0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb,
     0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b,
     0xf5ad9d0e, 0x8999220b, 0x27fb47b9
     };
main() {
     unsigned seed;
     int n;
     seed = 1;
     n = 128;
     (void)initstate(seed, (char *)state0, n);
     printf("random() = %d0\en", random());
     (void)setstate((char *)state1);
     printf("random() = %d0\en", random());
}
.fi
.in -2

.SH ATTRIBUTES
.LP
See \fBattributes\fR(5) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Standard
_
MT-Level	See \fBNOTES\fR below.
.TE

.SH SEE ALSO
.LP
\fBarc4random\fR(3C), \fBdrand48\fR(3C), \fBrand\fR(3C), \fBattributes\fR(5),
\fBstandards\fR(5)
.SH NOTES
.LP
The \fBrandom()\fR and \fBsrandom()\fR functions are unsafe in multithreaded
applications.
.sp
.LP
Use of these functions in multithreaded applications is unsupported.
.sp
.LP
For \fBinitstate()\fR and \fBsetstate()\fR, the \fIstate\fR argument must be
aligned on an \fBint\fR boundary.
.sp
.LP
\fBarc4random\fR(3C) is a newer and better performing random number generator.
Use it instead.