Using drand48_r as random number generator in C

So i've been writing some applications in C and using OpenMP for parallelization. I implemented a Monte-Carlo Pi estimate and found that the normal rand() function is not reentrent and thread-safe. The recommendation is to use the drand48_r option.

Now here is the problem, my application compiles fine on Linux eg. Ubuntu, Fedora and CentOS but does not compile on Mac OS X. The compile error on OS X is.

simple.c:7: error: storage size of ‘randBuffer’ isn’t known

The code used as the simple example is:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {

  double x;
  struct drand48_data randBuffer;

  srand48_r(time(NULL), &randBuffer);

  drand48_r(&randBuffer, &x);

  printf("Random number: %f\n", x);

  return EXIT_SUCCESS;

I read up about it and I found a note in the man pages that,

These functions are GNU extensions and are not portable.

Here is a link to it:

So I have a number of questions;

  1. What are GNU extensions and what makes it non portable?
  2. What alternatives do I have for random number generation on OS X that is also thread-safe?

Well that is about it.

The example is compiled with gcc as,

gcc simple.c -o simple


I don't really see the gain of using drand48_r over erand48. erand48 has the same type of random generator as drand48 but compared to that receives the state of the random generator as a function parameter, so it perfectly does the job.

The _r extensions store the result in place (the second parameter) and return an error code that is always guaranteed to be 0. I don't see much use in all of this. I'd stick to the POSIX interfaces (in particular erand48).

Use the standard re-entrant functions from <stdlib.h>. They simply pre-date the ubiquitous _r suffix, so their names are:

 #include <stdlib.h>

 double drand48(void);

 double erand48(unsigned short xsubi[3]);

 long jrand48(unsigned short xsubi[3]);

 void lcong48(unsigned short param[7]);

 long lrand48(void);

 long mrand48(void);

 long nrand48(unsigned short xsubi[3]);

 unsigned short *seed48(unsigned short seed16v[3]);

 void srand48(long seedval);

The erand48() function takes a data structure (actually, an array of 3 unsigned shorts) as its state. So, you can implement drand48_r() for Mac OS X by bundling that into a locally defined drand48_data structure and making drand48_r() call erand48(), or you can go portable and use erand48() which is defined by POSIX and has been available since the 1980s on most Unix systems. Personally, I'd go for portability, but writing a simple drand48_r() is also straight-forward except for sorting out which header declares it.

Need Your Help

Frequency and cumulative frequency curve on the same graph in R

r plot ggplot2 frequency-distribution cumulative-frequency

Is there a way (in R with ggplot or otherwise) to draw frequency and cumulative frequency curves in a single column (two rows) i.e. one top of the other such that a given quartile can be shown on b...

How to output non-terminated char array to stdout

c io output

I've got an array of characters which could contain a NULL character.