/* Attempt evaluate pi by throwing darts at a square board and counting hits in a circular target area. Actually uses one quarter of a 2*2 square with a quadrant of a unit circle inside it. */ #include #include using namespace std; int main( ) { const int NUMBER_OF_POINTS=3000; const double RAND_SCALE = 1.0/RAND_MAX; double total =0.0; double in =0.0; for( double total=0.0; total < NUMBER_OF_POINTS ; total =total+1 ) { double x=rand() * RAND_SCALE; double y=rand() * RAND_SCALE; /* (x,y) is in circle if x*x+y*y is less than 1 */ if( x*x+y*y < 1.0) in=in+1; cout << 4.0 * in/total << endl; } }