Copyright © 1997 The Open Group

rand, rand_r - pseudo-random number generator

`#include <stdlib.h> int rand (void); void srand(unsigned int`

seed); int rand_r(unsigned int *seed);

Therand()function computes a sequence of pseudo-random integers in the range 0 to {RAND_MAX} with a period of at least 2^{32}.The

srand()function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls torand(). Ifsrand()is then called with the same seed value, the sequence of pseudo-random numbers will be repeated. Ifrand()is called before any calls tosrand()are made, the same sequence will be generated as whensrand()is first called with a seed value of 1.The implementation will behave as if no function defined in this document calls

rand()orsrand.The

rand()interface need not be reentrant.The

rand_r()function computes a sequence of pseudo-random integers in the range 0 to {RAND_MAX}. (The value of the {RAND_MAX} macro will be at least 32767.)If

rand_r()is called with the same initial value for the object pointed to byseedand that object is not modified between successive returns and calls torand_r(), the same sequence shall be generated.

Therand()function returns the next pseudo-random number in the sequence. Thesrand()function returns no value.The

rand_r()function returns a pseudo-random integer.

No errors are defined.

None.

Thedrand48()function provides a much more elaborate random number generator.The following code defines a pair of functions which could be incorporated into applications wishing to ensure that the same sequence of numbers is generated across different machines:

`static unsigned long int next = 1; int myrand(void) /* RAND_MAX assumed to be 32767 */ { next = next * 1103515245 + 12345; return((unsigned int)(next/65536) % 32768); } void mysrand(unsigned int seed) { next = seed; }`

None.

drand48(),srand(),<stdlib.h>.

rand()derived from Issue 1 of the SVID.

rand_r()derived from the POSIX Threads Extension (1003.1c-1995).

Copyright © 1997 The Open Group

[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]