DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

rand(S-osr5)


rand, srand -- simple random-number generator

Syntax

cc ... -lc

#include  <stdlib.h>

int rand (void); void srand(unsigned int seed);

Description

rand- simple random-number generator

srand- reset random-number generator

The rand function uses a multiplicative congruential random-number generator with period 2^32 that returns successive pseudo-random numbers in the range from 0 to (2^15)-1.

The srand function can be called at any time to reset the random-number generator to a random starting point. The generator is initially seeded with a value of 1.

Notes

The spectral properties of rand are limited. The drand48(S-osr5) function provides a much better, though more elaborate, random-number generator.

The following functions define the semantics of the functions rand and srand.

   static unsigned long int next = 1;
   int rand()
   {
        next = next * 1103515245 + 12345;
        return ((unsigned int)(next/65536) % 32768);
   }
   void srand(seed)
   unsigned int seed;
   {
        next = seed;
   }

Specifying the semantics makes it possible to reproduce the behavior of programs that use pseudo-random sequences. This facilitates the testing of portable applications in different implementations.

See also

drand48(S-osr5)

Standards conformance

rand and srand are conformant with:

X/Open Portability Guide, Issue 3, 1989 ;
ANSI X3.159-1989 Programming Language -- C ;
IEEE POSIX Std 1003.1-1990 System Application Program Interface (API) [C Language] (ISO/IEC 9945-1) ;
and NIST FIPS 151-1 .


© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005