Add Book to My BookshelfPurchase This Book Online

Chapter 16 - Miscellaneous Routines

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

Random Numbers
A number of applications occasionally require one or more random numbers. All versions of UNIX provide a pseudo-random number generator:
    #include <stdlib.h>
    int rand(void);
    void srand(int seed);
Before requesting any random numbers, the generator should be seeded by calling srand. The seed parameter should be an integer type; the output of getpid or time(0) is usually a good value. Each time srand is called with the same seed, the output of the random number generator will be the same.
The rand function returns a random number in the range 0 to 215-1.
Some versions of UNIX, usually those based on BSD, also supply random and srandom, with similar semantics.
System V versions of UNIX provide a number of other random number generators described in the drand48 manual page; because they are not portable to all versions of the operating system, they are not frequently used.

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback