Quote:
|
Originally Posted by Kris Verdeyen
Prove me wrong. 
|
Well, I tried. First I tried analytically, and had the same explanation as FizMan. However, when I tried to figure out why yours was wrong, I couldn't. So, I wrote a program to do it a bunch of times. In short (unless my algorithm is wrong), you're right, it is 1/2.
I've included the code below in case someone wants to review my work.
Code:
/** @mainpage license
* Copyright (c) 2004 Joseph Ross
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/** @file smiths.c
*
* @author Joseph Ross, christmasboy_81@rossesmail.com
* @date 7/7/2004
*
* This program attempts to solve the smith's children problem as defined as
* problem #8 in this thread on chiefdelphi.com:
* http://www.chiefdelphi.com/forums/sh...3&page=1&pp=15
*
* The problem is as follows;
* Mr and Mrs Smith have two children. You ring at their door, and one of their
* two children, a girl opens the door. What is the probability that the other
* child is also a girl?
*
* The problem is also stated at
* http://www.julienstern.org/riddle_sol.php3?id=7 and gives the answer as 1/3
*
* The algorithm used is as follows;
*
* @li assign two kids as boys or girls randomly and idependently of each other
* @li pick one of the two to answer the door.
* @li if the one picked is a girl, check if the other one is a girl and record
* the number of times of each.
* @li divide the number of girls from the first pick by the number of girls
* from the second pick.
*
* The answer, as given by this program is 1/2.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/** The 1 is a Girl (0 is a boy) */
#define GIRL 1
/** Calculates the answer to the smiths children problem
*
* @param argc the number of arguments to the program.
* @param *argv[] array of pointers to the arguments of the program.
*
* @retval 0 no error.
* @retval 1 no argument given.
* @retval 2 invalid number of iterations.
*
*/
int main(int argc, char *argv[])
{
int i; // counter.
int kids[2]; // array of two kids.
int its = 0; // number of iterations to run.
int second_girls = 0; // number of times the second kid was a girl.
int first_girls = 0; // number of times the first kid was a girl.
int first_kid = 0; // which kid answered the door.
if (argc != 2)
{
printf("usage: smiths <number of iterations>\n");
return(1);
}
its = atoi(argv[1]);
if (its <= 0)
{
printf("usage: smiths <number of iterations>\n");
printf("number of iterations must be greater then 0\n");
return(2);
}
srandom(time(NULL));
for(i=0;i<its;i++)
{
//pick the gender of the kids
kids[0] = random() % 2;
kids[1] = random() % 2;
//pick which kid opens the door
first_kid = random() % 2;
//count first girl and check second kid if first kid is girl
if (kids[first_kid] == GIRL)
{
first_girls++;
//check second kid
if (first_kid == 0 && kids[1] == GIRL)
{
second_girls++;
}
else if (first_kid == 1 && kids[0] == GIRL)
{
second_girls++;
}
}
}
//print results
printf("iterations:\t%i\n", its);
printf("first girls:\t%i\n", first_girls);
printf("second girls:\t%i\n", second_girls);
printf("%% second girls:\t%f\n", (double)second_girls/first_girls);
return(0);
}