title: Random numbers from Beta distribution, C++
body: <p>I've written a simulation in C++ that generates (1,000,000)^2 numbers from a specific probability distribution and then does something with them. So far I've used Exponential, Normal, Gamma, Uniform and Poisson distributions. Here is the code for one of them:</p>
  
  <pre><code>#include &lt;boost/random.hpp&gt;
  
  ...main...
  
      srand(time(NULL)) ;
      seed = rand();
      boost::random::mt19937 igen(seed) ;
      boost::random::variate_generator&lt;boost::random::mt19937, boost::random::normal_distribution&lt;&gt; &gt;
      norm_dist(igen, boost::random::normal_distribution&lt;&gt;(mu,sigma)) ;
  </code></pre>
  
  <p>Now I need to run it for the Beta distribution. All of the distributions I've done so far took 10-15 hours. The Beta distribution is not in the boost/random package so I had to use the boost/math/distributions package. I found <a href="https://stackoverflow.com/questions/4181403/generate-random-number-based-on-beta-distribution-using-boost">this page on StackOverflow</a> which proposed a solution. Here it is (copy-pasted):</p>
  
  <pre><code>#include &lt;boost/math/distributions.hpp&gt; 
  using namespace boost::math;  
  double alpha, beta, randFromUnif;  
  //parameters and the random value on (0,1) you drew  
  beta_distribution&lt;&gt; dist(alpha, beta); 
  double randFromDist = quantile(dist, randFromUnif); 
  </code></pre>
  
  <p>I replicated it and it worked. The run time estimates of my simulation are linear and accurately predictable. They say that this will run for 25 days. I see two possibilities:
  1. the method proposed is inferior to the one I was using previously for other distributions
  2. the Beta distribution is just much harder to generate random numbers from</p>
  
  <p>Bare in mind that I have below minimal understanding of C++ coding, so the questions I'm asking may be silly. I can't wait for a month for this simulation to complete, so is there anything I can do to improve that? Perhaps use the initial method that I was using and modify it to work with the boost/math/distributions package? I don't even know if that's possible. </p>
  
  <p>Another piece of information that may be useful is that the parameters are the same for all (1,000,000)^2 of the numbers that I need to generate. I'm saying this because the Beta distribution does have a nasty PDF and perhaps the knowledge that the parameters are fixed can somehow be used to simplify the process? Just a random guess.</p>