// RandDemo8.cc // features: // * uses random() to get a random integer // * repetition with 'for' // * conditional with 'if' #include using namespace std; int main() { double x, y ; int N=100, count_in=0 , seed=123 ; double fraction_in ; // Initialise random number generator srandom(seed); // Perform N experiments. for(int n=1; n<=N; n++) { x = ((double)random()/(1.0+(double)RAND_MAX)) ; y = ((double)random()/(1.0+(double)RAND_MAX)); if ( x*x + y*y < 1.0 ) { count_in ++ ; } } // Integer variables must be converted (cast) for correct division fraction_in = static_cast(count_in)/N; cout << count_in << "/" << N << endl; cout << fraction_in << endl ; cout << 4.0 * fraction_in << endl; return 0; }