Copyright 1989 AT&T. Copyright (c) 2004, Sun Microsystems, Inc. All Rights Reserved.
The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
#include <stdlib.h> int rand(void);
void srand(unsigned int seed);
int rand_r(unsigned int *seed);
The rand() function uses a multiplicative congruential random-number generator with period 2^32 that returns successive pseudo-random numbers in the range of 0 to RAND_MAX (defined in <stdlib.h>).
The srand() function uses the argument seed as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand(). If srand() is then called with the same seed value, the sequence of pseudo-random numbers will be repeated. If rand() is called before any calls to srand() have been made, the same sequence will be generated as when srand() is first called with a seed value of 1.
The rand_r() function has the same functionality as rand() except that a pointer to a seed seed must be supplied by the caller. If rand_r() is called with the same initial value for the object pointed to by seed and that object is not modified between successive calls to rand_r(), the same sequence as that produced by calls to rand() will be generated.
The rand() and srand() functions provide per-process pseudo-random streams shared by all threads. The same effect can be achieved if all threads call rand_r() with a pointer to the same seed object. The rand_r() function allows a thread to generate a private pseudo-random stream by having the seed object be private to the thread.
The spectral properties of rand() are limited. The arc4random(3C) function provides a better, more elaborate random-number generator.
When compiling multithreaded applications, the _REENTRANT flag must be defined on the compile line. This flag should be used only in multithreaded applications.
See attributes(5) for descriptions of the following attributes:
ATTRIBUTE TYPE ATTRIBUTE VALUE |
Interface Stability Standard |
MT-Level Safe |
arc4random(3C), attributes(5), standards(5)