2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3 .\" permission to reproduce portions of its copyrighted documentation.
4 .\" Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
7 .\" The Institute of Electrical and Electronics Engineers and The Open
8 .\" Group, have given us permission to reprint portions of their
11 .\" In the following statement, the phrase ``this text'' refers to portions
12 .\" of the system documentation.
14 .\" Portions of this text are reprinted and reproduced in electronic form
15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16 .\" Standard for Information Technology -- Portable Operating System
17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19 .\" Engineers, Inc and The Open Group. In the event of any discrepancy
20 .\" between these versions and the original IEEE and The Open Group
21 .\" Standard, the original IEEE and The Open Group Standard is the referee
22 .\" document. The original Standard can be obtained online at
23 .\" http://www.opengroup.org/unix/online.html.
25 .\" This notice shall appear on any product containing this material.
27 .\" The Berkeley software License Agreement specifies the terms and conditions
28 .\" for redistribution.
31 .\" Copyright (c) 1983 Regents of the University of California.
32 .\" All rights reserved.
33 .\" Portions Copyright (c) 1992, X/Open Company Limited. All Rights Reserved.
34 .\" Copyright (c) 2002, Sun Microsystems, Inc. All Rights Reserved.
36 .TH RANDOM 3C "Aug 14, 2002"
38 random, srandom, initstate, setstate \- pseudorandom number functions
44 \fBlong\fR \fBrandom\fR(\fBvoid\fR);
49 \fBvoid\fR \fBsrandom\fR(\fBunsigned int\fR \fIseed\fR);
54 \fBchar *\fR\fBinitstate\fR(\fBunsigned int\fR \fIseed\fR, \fBchar\fR \fI*state\fR, \fBsize_t\fR \fIsize\fR);
59 \fBchar *\fR\fBsetstate\fR(\fBconst char *\fR\fIstate\fR);
64 The \fBrandom()\fR function uses a nonlinear additive feedback random-number
65 generator employing a default state array size of 31 long integers to return
66 successive pseudo-random numbers in the range from 0 to 2^31 \(mi1. The period
67 of this random-number generator is approximately 16 x (2^31 \(mi1). The size of
68 the state array determines the period of the random-number generator.
69 Increasing the state array size increases the period.
72 The \fBsrandom()\fR function initializes the current state array using the
76 The \fBrandom()\fR and \fBsrandom()\fR functions have (almost) the same calling
77 sequence and initialization properties as \fBrand()\fR and \fBsrand()\fR (see
78 \fBrand\fR(3C)). The difference is that \fBrand\fR(3C) produces a much less
79 random sequence\(emin fact, the low dozen bits generated by rand go through a
80 cyclic pattern. All the bits generated by \fBrandom()\fR are usable.
83 The algorithm from \fBrand()\fR is used by \fBsrandom()\fR to generate the 31
84 state integers. Because of this, different \fBsrandom()\fR seeds often produce,
85 within an offset, the same sequence of low order bits from \fBrandom()\fR. If
86 low order bits are used directly, \fBrandom()\fR should be initialized with
87 \fBsetstate()\fR using high quality random values.
90 Unlike \fBsrand()\fR, \fBsrandom()\fR does not return the old seed because the
91 amount of state information used is much more than a single word. Two other
92 routines are provided to deal with restarting/changing random number
93 generators. With 256 bytes of state information, the period of the
94 random-number generator is greater than 2^69, which should be sufficient for
98 Like \fBrand\fR(3C), \fBrandom()\fR produces by default a sequence of numbers
99 that can be duplicated by calling \fBsrandom()\fR with 1 as the seed.
102 The \fBinitstate()\fR and \fBsetstate()\fR functions handle restarting and
103 changing random-number generators. The \fBinitstate()\fR function allows a
104 state array, pointed to by the \fIstate\fR argument, to be initialized for
105 future use. The \fBsize\fR argument, which specifies the size in bytes of the
106 state array, is used by \fBinitstate()\fR to decide what type of random-number
107 generator to use; the larger the state array, the more random the numbers.
108 Values for the amount of state information are 8, 32, 64, 128, and 256 bytes.
109 Other values greater than 8 bytes are rounded down to the nearest one of these
110 values. For values smaller than 8, \fBrandom()\fR uses a simple linear
111 congruential random number generator. The \fIseed\fR argument specifies a
112 starting point for the random-number sequence and provides for restarting at
113 the same point. The \fBinitstate()\fR function returns a pointer to the
114 previous state information array.
117 If \fBinitstate()\fR has not been called, then \fBrandom()\fR behaves as though
118 \fBinitstate()\fR had been called with \fIseed\fR\|=\|1 and \fIsize\fR\|=\|128.
121 If \fBinitstate()\fR is called with \fIsize\fR\|<\|8, then \fBrandom()\fR uses
122 a simple linear congruential random number generator.
125 Once a state has been initialized, \fBsetstate()\fR allows switching between
126 state arrays. The array defined by the \fIstate\fR argument is used for further
127 random-number generation until \fBinitstate()\fR is called or \fBsetstate()\fR
128 is called again. The \fBsetstate()\fR function returns a pointer to the
129 previous state array.
132 For a more powerful random number generator, see \fBarc4random\fR(3C).
135 The \fBrandom()\fR function returns the generated pseudo-random number.
138 The \fBsrandom()\fR function returns no value.
141 Upon successful completion, \fBinitstate()\fR and \fBsetstate()\fR return a
142 pointer to the previous state array. Otherwise, a null pointer is returned.
145 No errors are defined.
148 After initialization, a state array can be restarted at a different point in
154 The \fBinitstate()\fR function can be used, with the desired seed, state array,
155 and size of the array.
161 The \fBsetstate()\fR function, with the desired state, can be used, followed by
162 \fBsrandom()\fR with the desired seed. The advantage of using both of these
163 functions is that the size of the state array does not have to be saved once it
168 \fBExample 1 \fRInitialize an array.
171 The following example demonstrates the use of \fBinitstate()\fR to intialize an
172 array. It also demonstrates how to initialize an array and pass it to
179 static unsigned int state0[32];
180 static unsigned int state1[32] = {
182 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342,
183 0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd,
184 0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86,
185 0xda672e2a, 0x1588ca88, 0xe369735d, 0x904f35f7,
186 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc,
187 0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb,
188 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b,
189 0xf5ad9d0e, 0x8999220b, 0x27fb47b9
196 (void)initstate(seed, (char *)state0, n);
197 printf("random() = %d0\en", random());
198 (void)setstate((char *)state1);
199 printf("random() = %d0\en", random());
206 See \fBattributes\fR(5) for descriptions of the following attributes:
214 ATTRIBUTE TYPE ATTRIBUTE VALUE
216 Interface Stability Standard
218 MT-Level See \fBNOTES\fR below.
223 \fBarc4random\fR(3C), \fBdrand48\fR(3C), \fBrand\fR(3C), \fBattributes\fR(5),
227 The \fBrandom()\fR and \fBsrandom()\fR functions are unsafe in multithreaded
231 Use of these functions in multithreaded applications is unsupported.
234 For \fBinitstate()\fR and \fBsetstate()\fR, the \fIstate\fR argument must be
235 aligned on an \fBint\fR boundary.
238 \fBarc4random\fR(3C) is a newer and better performing random number generator.