/* !----------------------------------------------------- ! Simulated frequency experiment; ! ! Each switch in the following circuit independently ! has probability of 0.5 of being in the "on" state. ! The probabilty of points a and b being electrically ! connected is then 0.6875. ! ! +--------- s ---------+ ! | | ! a---| |---b ! | +-- s --+ | ! +-- s ----| |---+ ! +-- s --+ !----------------------------------------------------- */ #include #include #include using namespace std; int main() { srand ( time(NULL) ); // Randomize the RNG seed. int n=100000000; // n trials int h=0; // Number of successes. for (int j=1; j<=n; j++) { int s1 = rand() % 2; int s2 = rand() % 2; int s3 = rand() % 2; int s4 = rand() % 2; if ( s1 || ( s2 && ( s3 || s4 ) ) ) h++; } cout << fixed << setprecision(9) << setw(12) << h/double(n) << endl; }