title: Optimize estimating Pi function C++
body: <p>I've wrote a program, that approximates Pi using the Monte-Carlo method. It is working fine, but I wonder if I can make it work better and faster, because when inserting something like ~<code>n = 100000000</code> and bigger from that point, it takes some time to do the calculations and print the result.</p>
  <p>I've imagined how I could try to approximate it better by doing a median for <code>n</code> results, but considering how slow my algorithm is for big numbers, I decided not doing so.</p>
  <p>Basically, the question is: How can I make this function work faster?</p>
  <p>Here is the code that I've got so far:</p>
  <pre class="lang-cpp prettyprint-override"><code>double estimate_pi(double n)
  {
      int i;
      double x,y, distance;
      srand( (unsigned)time(0));
      double num_point_circle = 0;
      double num_point_total = 0;
      double final;
  
      for (i=0; i&lt;n; i++)
      {
          x = (double)rand()/RAND_MAX;
          y = (double)rand()/RAND_MAX;
          distance = sqrt(x*x + y*y);
          if (distance &lt;= 1)
          {
              num_point_circle+=1;
          }
          num_point_total+=1;
      }
      final = ((4 * num_point_circle) / num_point_total);
      return final;
  }
  </code></pre>