'\" te .\" Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. .\" Copyright 1989 AT&T .TH rand 3C "24 May 2011" "SunOS 5.11" "Standard C Library Functions" .SH NAME rand, srand, rand_r \- simple random-number generator .SH SYNOPSIS .LP .nf #include \fBint\fR \fBrand\fR(\fBvoid\fR); .fi .LP .nf \fBvoid\fR \fBsrand\fR(\fBunsigned int\fR \fIseed\fR); .fi .LP .nf \fBint\fR \fBrand_r\fR(\fBunsigned int *\fR\fIseed\fR); .fi .SH DESCRIPTION .sp .LP The \fBrand()\fR function uses a multiplicative congruential random-number generator with period 2^32 that returns successive pseudo-random numbers in the range of 0 to \fBRAND_MAX\fR (defined in <\fBstdlib.h\fR>). .sp .LP The \fBsrand()\fR function uses the argument \fIseed\fR as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to \fBrand()\fR. If \fBsrand()\fR is then called with the same \fIseed\fR value, the sequence of pseudo-random numbers will be repeated. If \fBrand()\fR is called before any calls to \fBsrand()\fR have been made, the same sequence will be generated as when \fBsrand()\fR is first called with a \fIseed\fR value of 1. .sp .LP The \fBrand_r()\fR function has the same functionality as \fBrand()\fR except that a pointer to a seed \fIseed\fR must be supplied by the caller. If \fBrand_r()\fR is called with the same initial value for the object pointed to by seed and that object is not modified between successive calls to \fBrand_r()\fR, the same sequence as that produced by calls to \fBrand()\fR will be generated. .sp .LP The \fBrand()\fR and \fBsrand()\fR functions provide per-process pseudo-random streams shared by all threads. The same effect can be achieved if all threads call \fBrand_r()\fR with a pointer to the same seed object. The \fBrand_r()\fR function allows a thread to generate a private pseudo-random stream by having the seed object be private to the thread. .SH USAGE .sp .LP The spectral properties of \fBrand()\fR are limited. The \fBdrand48\fR(3C) function provides a better, more elaborate random-number generator. .sp .LP When compiling multithreaded applications, the \fB_REENTRANT\fR flag must be defined on the compile line. This flag should be used only in multithreaded applications. .sp .LP Programmers should use \fB/dev/urandom\fR or \fB/dev/random\fR for most random-number generation, especially for cryptographic purposes. See \fBrandom\fR(7D). .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 StabilityCommitted _ MT-LevelSafe _ StandardSee \fBstandards\fR(5). .TE .SH SEE ALSO .sp .LP \fBdrand48\fR(3C), \fBattributes\fR(5), \fBstandards\fR(5), \fBrandom\fR(7D)