drand48() — Pseudo-random number generator

Standards

Standards / Extensions C or C++ Dependencies

XPG4
XPG4.2
Single UNIX Specification, Version 3

both  

Format

#define _XOPEN_SOURCE
#include <stdlib.h>

double drand48(void);

General description

The drand48(), erand48(), jrand48(), lrand48(), mrand48() and nrand48() functions generate uniformly distributed pseudo-random numbers using a linear congruential algorithm and 48-bit integer arithmetic.

The functions drand48() and erand48() return nonnegative, double-precision, floating-point values, uniformly distributed over the interval [0.0,1.0). These functions have been extended so that the returned value will be in the proper floating-point format (hexadecimal or IEEE) based on the floating-point mode of the invoking thread.

The functions lrand48() and nrand48() return nonnegative, long integers, uniformly distributed over the interval [0,2**31).

The functions mrand48() and jrand48() return signed long integers, uniformly distributed over the interval [-2**31,2**31).

The drand48() function generates the next 48-bit integer value in a sequence of 48-bit integer values, X(i), according to the linear congruential formula:
X(n+1) = (aX(n) + c)mod(2**48)     n>=0
The initial values of X, a, and c are:
X(0) = 1
a   = 5deece66d (base 16)
c   = b         (base 16)

C/370™ provides storage to save the most recent 48-bit integer value of the sequence, X(i). This storage is shared by the drand48(), lrand48() and mrand48() functions. The value, X(n), in this storage may be reinitialized by calling the lcong48(), seed48() or srand48() function. Likewise, the values of a and c, may be changed by calling the lcong48() function. Thereafter, whenever the seed48() or srand48() function is called to change X(n), the initial values of a and c are also reestablished.

Special behavior for z/OS® UNIX Services: You can make the drand48() function and other functions in the drand48 family thread-specific by setting the environment variable _RAND48 to the value THREAD before calling any function in the drand48 family.

If you do not request thread-specific behavior for the drand48 family, C/370 serializes access to the storage for X(n), a and c by functions in the drand48 family when they are called by a multithreaded application.

If thread-specific behavior is requested, and the drand48() function is called from thread t, the drand48() function generates the next 48-bit integer value in a sequence of 48-bit integer values, X(t,i), for the thread t. The sequence of values for a thread is generated according to the linear congruential formula:
   X(t,n+1) = (a(t)X(t,n) + c(t))mod(2**48)     n>=0
The initial values of X(t), a(t) and c(t) for the thread t are:
   X(t,0) = 1
   a(t)   = 5deece66d (base 16)
   c(t)   = b         (base 16)

C/370 provides storage which is specific to the thread t to save the most recent 48-bit integer value of the sequence, X(t,i), generated by the drand48(), lrand48() or mrand48() function. The value, X(t,n), in this storage may be reinitialized by calling the lcong48(), seed48() or srand48() function from the thread t. Likewise, the values of a(t) and c(t) for thread t may be changed by calling the lcong48() function from the thread. Thereafter, whenever the seed48() or srand48() function is called from the thread t to change X(t,n), the initial values of a(t) and c(t) are also reestablished.

Returned value

drand48() transforms the generated 48-bit value, X(n+1), to a double-precision, floating-point value on the interval [0.0,1.0) and returns this transformed value.

Special behavior for z/OS UNIX Services: If thread-specific behavior is requested for the drand48 family and rand48() is called on thread t, drand48() transforms the generated 48-bit value, X(t,n+1), to a double-precision, floating-point value on the interval [0.0,1.0) and returns this transformed value.

Related information