/* A couple decides to continue to have children until they have at least one boy and one girl. Then they decide to stop having children. This program simulates counting how many children the couple might have. */ #include /* libraries for the pseudo-random number generator */ #include #include /* Within the time.h library, * time returns a value based upon the time of day * Within the stdlib.h library * rand returns a pseudo-random integer between 0 and RAND_MAX */ int main () { /* initialize pseudo-random number generator */ /* change the seed to the pseudo-random number generator, based on the time of day */ srand (time ((time_t *) 0) ); /* couple starts with no children */ int boys = 0; int girls = 0; /* couple has children */ while ((boys == 0) || (girls == 0)) { if ((((double) rand()) / ((double) RAND_MAX)) < 0.5) boys++; else girls++; } /* reporting of family size */ printf ("Simulation of family size\n"); printf (" boys: %2d girls: %2d total: %2d\n", boys, girls, boys + girls); return 0; }