.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk) .\" .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" .\" References consulted: .\" Linux libc source code .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991) .\" 386BSD man pages .\" Modified Sun Mar 28 00:25:51 1993, David Metcalfe .\" Modified Sat Jul 24 18:13:39 1993 by Rik Faith (faith@cs.unc.edu) .TH RANDOM 3 "March 28, 1993" "GNU" "Linux Programmer's Manual" .SH NAME random, srandom, initstate, setstate \- random number generator. .SH SYNOPSIS .nf .B #include .sp .B long int random(void); .nl .BI "void srandom(unsigned int " seed ); .nl .BI "char *initstate(unsigned int " seed ", char *" state ", size_t " n ); .nl .BI "char *setstate(char *" state ); .fi .SH DESCRIPTION The \fBrandom()\fP function uses a non-linear additive feedback random number generator employing a default table of size 31 long integers to return successive pseudo-random numbers in the range from 0 to \fBRAND_MAX\fR. The period of this random number generator is very large, approximately 16*((2**31)\-1). .PP The \fBsrandom()\fP function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by \fBrandom()\fP. These sequences are repeatable by calling \fBsrandom()\fP with the same seed value. If no seed value is provided, the \fBrandom()\fP function is automatically seeded with a value of 1. .PP The \fBinitstate()\fP function allows a state array \fIstate\fP to be initialized for use by \fBrandom()\fP. The size of the state array \fIn\fP is used by \fBinitstate()\fP to decide how sophisticated a random number generator it should use \(em the larger the state array, the better the random numbers will be. \fIseed\fP is the seed for the initialization, which specifies a starting point for the random number sequence, and provides for restarting at the same point. .PP The \fBsetstate()\fP function changes the state array used by the \fBrandom()\fP function. The state array \fIstate\fP is used for random number generation until the next call to \fBinitstate()\fP or \fBsetstate()\fP. \fIstate\fP must first have been initialized using \fBinitstate()\fP. .SH "RETURN VALUE" The \fBrandom()\fP function returns a value between 0 and RAND_MAX. The \fBsrandom()\fP function returns no value. The \fBinitstate()\fP and \fBsetstate()\fP functions return a pointer to the previous state array. .SH "ERRORS" .TP .B EINVAL A state array of less than 8 bytes was specified to \fBinitstate()\fP. .SH NOTES Current "optimal" values for the size of the state array \fIn\fP are 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to the nearest known amount. Using less than 8 bytes will cause an error. .SH "CONFORMING TO" BSD 4.3 .SH "SEE ALSO" .BR rand "(3), " srand (3)