erand48_r - Linux


erand48_r is a C library function that generates a random number using the erand48 family of pseudorandom number generators. It is designed to be reentrant and thread-safe, making it suitable for use in multithreaded applications.


int erand48_r(struct drand48_data *buf, double *result);


  • buf: Pointer to a drand48_data structure holding the internal state of the random number generator.
  • result: Pointer to the variable to store the generated random number.

Return Value:

The function returns 0 on success, or -1 if an error occurs.




Simple Usage:

#include <stdlib.h>

int main() {
  struct drand48_data buf;
  srand48_r(1234, &buf);
  double r = erand48_r(&buf);

  printf("Random number: %f\n", r);
  return 0;

Common Issues

Incorrect Initialization:

  • Ensure that you initialize the drand48_data structure using srand48_r before calling erand48_r.

Thread Safety:

  • The erand48_r function is reentrant and can be safely used in multithreaded applications. However, each thread should use its own drand48_data structure.


Generating Random Data:

  • erand48_r can be used in scripts or programs that require random data, such as simulations, testing, and games.

Related Commands

  • rand: Generates a random integer.
  • srand: Initializes the random number generator.
  • drand48: Generates a double-precision random number.
  • srand48_r: Initializes the drand48_data structure for the erand48 family of random number generators.