title: Using drand48_r as random number generator in C
body: <p>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.</p>
  
  <p>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.</p>
  
  <blockquote>
    <p>simple.c:7: error: storage size of ‘randBuffer’ isn’t known</p>
  </blockquote>
  
  <p>The code used as the simple example is:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(int argc, char* argv[]) {
  
    double x;
    struct drand48_data randBuffer;
  
    srand48_r(time(NULL), &amp;randBuffer);
  
    drand48_r(&amp;randBuffer, &amp;x);
  
    printf("Random number: %f\n", x);
  
    return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>I read up about it and I found a note in the man pages that,</p>
  
  <blockquote>
    <p>These functions are GNU extensions and are not portable.</p>
  </blockquote>
  
  <p>Here is a link to it: <a href="http://www.kernel.org/doc/man-pages/online/pages/man3/drand48_r.3.html" rel="nofollow">http://www.kernel.org/doc/man-pages/online/pages/man3/drand48_r.3.html</a></p>
  
  <p>So I have a number of questions;</p>
  
  <ol>
  <li>What are GNU extensions and what makes it non portable?</li>
  <li>What alternatives do I have for random number generation on OS X that is also thread-safe?</li>
  </ol>
  
  <p>Well that is about it.</p>
  
  <p>The example is compiled with gcc as,</p>
  
  <blockquote>
    <p>gcc simple.c -o simple</p>
  </blockquote>