Sun Microsystems, Inc.
spacerspacer
spacer   www.sun.com docs.sun.com | | |  
spacer
black dot
   
A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W   X   Y   Z
    
 
Standard C Library Functionsrand(3C)


NAME

 rand, srand, rand_r - simple random-number generator

SYNOPSIS

 
#include <stdlib.h>
int rand(void);
 void srand(unsigned int seed);
 int rand_r(unsigned int *seed);

DESCRIPTION

 

The rand() function uses a multiplicative congruential random-number generator with period 232 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. The seed to be supplied is not the same seed as in srand().

USAGE

 

The spectral properties of rand() are limited. The drand48(3C) function provides a better, more elaborate random-number generator.

The rand() is unsafe in multithreaded applications. The rand_r() function is MT-Safe, and should be used instead. The srand() function is unsafe in multithreaded applications.

When compiling multithreaded applications, the _REENTRANT flag must be defined on the compile line. This flag should only be used in multithreaded applications.

ATTRIBUTES

 

See attributes(5) for descriptions of the following attributes:

ATTRIBUTE TYPEATTRIBUTE VALUE
MT-LevelSee USAGE above.

SEE ALSO

 

drand48(3C), attributes(5)


SunOS 5.9Go To TopLast Changed 25 Jul 2000

 
      
      
Copyright 2002 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.