#include <stdlib.h> /* random() */
#include <stdio.h> /* printf() */
#include <time.h> /* time() */
#include <math.h> /* floor() */
#define RANDMAX1 2147483648 /* 2**31 = RAND_MAX + 1 for random() */
#define SIMS 1000000000 /* number of simulations */
#define SEXS 2
#define DAYS 7
enum Sex {GIRL, BOY};
enum Day {MON, TUE, WED, THU, FRI, SAT, SUN};
double drand(void); /* generate random number 0 <= x < 1 */
int main (int argc, const char * argv[]) {
int sim, i, n, k, boy, hit, sex, day;
/* system init */
srandom((unsigned)time(NULL));
n = 0;
k = 0;
/* simulations loop */
for (sim = 1; sim <= SIMS; ++sim) {
boy = 0;
hit = 0;
for (i = 0; i < 2; ++i) {
sex = (int)floor(drand() * SEXS);
day = (int)floor(drand() * DAYS);
boy += sex;
if (sex == BOY && day == TUE)
++hit;
}
if (hit == 1) { /* Not both boys born on a Tue */
++n;
if (boy == 2) /* Is the other one a boy as well? */
++k;
}
/* output */
if (!(sim % 100000000))
printf("%i: %i / %i = %lf\n", sim, k, n, (double)k/(double)n);
}
return 0;
}
double drand(void) {
return ((double)random() / ((double)RANDMAX1));
}