.\" Copyright (c) 2001-2003 The Open Group, All Rights Reserved .TH "RAND" P 2003 "IEEE/The Open Group" "POSIX Programmer's Manual" .\" rand .SH NAME rand, rand_r, srand \- pseudo-random number generator .SH SYNOPSIS .LP \fB#include .br .sp int rand(void); .br \fP .LP \fBint rand_r(unsigned *\fP\fIseed\fP\fB); \fP \fB .br void srand(unsigned\fP \fIseed\fP\fB); .br \fP .SH DESCRIPTION .LP The \fIrand\fP() function shall compute a sequence of pseudo-random integers in the range [0, {RAND_MAX}] \ with a period of at least 2**32. .LP The \fIrand\fP() function need not be reentrant. A function that is not required to be reentrant is not required to be thread-safe. .LP The \fIrand_r\fP() function shall compute a sequence of pseudo-random integers in the range [0, {RAND_MAX}]. (The value of the {RAND_MAX} macro shall be at least 32767.) .LP If \fIrand_r\fP() is called with the same initial value for the object pointed to by \fIseed\fP and that object is not modified between successive returns and calls to \fIrand_r\fP(), the same sequence shall be generated. .LP The \fIsrand\fP() function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to \fIrand\fP(). If \fIsrand\fP() is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated. If \fIrand\fP() is called before any calls to \fIsrand\fP() are made, the same sequence shall be generated as when \fIsrand\fP() is first called with a seed value of 1. .LP The implementation shall behave as if no function defined in this volume of IEEE\ Std\ 1003.1-2001 calls \fIrand\fP() or \fIsrand\fP(). .SH RETURN VALUE .LP The \fIrand\fP() function shall return the next pseudo-random number in the sequence. .LP The \fIrand_r\fP() function shall return a pseudo-random integer. .LP The \fIsrand\fP() function shall not return a value. .SH ERRORS .LP No errors are defined. .LP \fIThe following sections are informative.\fP .SH EXAMPLES .SS Generating a Pseudo-Random Number Sequence .LP The following example demonstrates how to generate a sequence of pseudo-random numbers. .sp .RS .nf \fB#include #include \&... long count, i; char *keystr; int elementlen, len; char c; \&... /* Initial random number generator. */ srand(1); .sp /* Create keys using only lowercase characters */ len = 0; for (i=0; i\fP .SH COPYRIGHT Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version 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 .