#include <iostream>
#include <math.h>


using namespace std;

struct point_t
{
  float x;
  float y;
};

// function to calculate a uniformly distributed random number 
// between min and max
double unirandom(float min, float max)
{
  return min + (max-min)*random() / (pow(2., 31.)-1.);
}

int main(int argc, char* argv[])
{
  if (argc < 2) {
    cout << "error: number of points must be given as argument" << endl;
    return -1;
  }

  int npoints = atoi(argv[1]);

  point_t * points = new point_t[npoints];

  int naccepted = 0;
  for (int i=0; i<npoints; i++) {
    points[i].x = unirandom(0., 1.);
    points[i].y = unirandom(0., 1.);

    if (hypot( points[i].x, points[i].y) < 1.) {
      naccepted++;
    }
  }

  cout << "pi = " << 4.*float(naccepted)/float(npoints) << endl;

  delete[] points;
}