'\" te .\" Copyright (c) 1983 Regents of the University of California. All rights reserved. The Berkeley software License Agreement specifies the terms and conditions for redistribution. Copyright (c) 2002, Sun Microsystems, Inc. All Rights Reserved Portions Copyright (c) 1992, .\" X/Open Company Limited All Rights Reserved .\" 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 Sun OS 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. .TH random 3C "14 Aug 2002" "SunOS 5.11" "Standard C Library Functions" .SH NAME random, srandom, initstate, setstate \- pseudorandom number functions .SH SYNOPSIS .LP .nf #include \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 .sp .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. .SH RETURN VALUES .sp .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 .sp .LP No errors are defined. .SH USAGE .sp .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 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 .sp .LP See \fBattributes\fR(5) for descriptions of the following attributes: .sp .sp .TS tab() box; cw(2.75i) |cw(2.75i) lw(2.75i) |lw(2.75i) . ATTRIBUTE TYPEATTRIBUTE VALUE _ Interface StabilityStandard _ MT-LevelSee \fBNOTES\fR below. .TE .SH SEE ALSO .sp .LP \fBdrand48\fR(3C), \fBrand\fR(3C), \fBattributes\fR(5), \fBstandards\fR(5) .SH NOTES .sp .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 Newer and better performing random number generators such as \fBaddrans()\fR and \fBlcrans()\fR are available with the SUNWspro package.