// Assuming there are 3 bags (each with (Red,Red), (Red,White), and (White,White)) // assuming these three bags occur uniformly, ie. each with probability 1/3 #include #include #include void main() { long i; int draw1, draw2, choice, tmp; long totalCount=0L, redCount=0L; srand(time(NULL)); for (i=0; i<100000L; i++) { draw1 = rand() % 3; // take one kind of bags if (draw1 == 0) // (Red, Red) { totalCount++; redCount++; } else if (draw1 == 1) // (Red, White) { draw2 = rand() % 2; if (draw2 == 0) // the first is Red totalCount++; else // the first is White /* do nothing */; } } printf("Pr(2nd is red | 1st is red)=%lf\n", (double)redCount / (double)totalCount); }