/* This program calcutates pi using a monte carlo simulation * * Mark Monarch * 2/25/15 */ #include #include #include #include using namespace std; int main() { // prepares random number generator srand(time(NULL)); // declare variables float x, y, piEstimate; long trials, inCircle = 0; // asks user for number of trials. The more trials, the more accurate it will be cout << "Input number of trials: "; cin >> trials; // runs the trials for (int i = 0; i < trials; i++) { // randomly gets x value x = (float)(rand() % 9999) / 9999.0; // randomly gets y value y = (float)(rand() % 9999) / 9999.0; if (x*x+y*y < 1) // if point is in the circle { inCircle++; } } // finds the ratios between the areas piEstimate = 4.0 * (float) inCircle / (float) trials; // display pi estimate cout << endl << "Pi is approximately: " << piEstimate << endl; // Waits for user system("PAUSE"); // return return 0; }