X-Message-Number: 7072
Date: Thu, 24 Oct 1996 20:47:48 -0700
From: Hara Ra <>
Subject: Goats and Cars

Ok you people - there's nothing like flawed logic to stimulate
irrational discussions. Here's a simulation written in Borland C.

The cars are selected about 2/3 of the time. Check it out.

O---------------------------------O
|  Hara Ra <> |
|  Box 8334 Santa Cruz, CA 95061  |
O---------------------------------O

/* simulation of goats puzzle - Hara Ra */

#include <conio.h>	/* console I/O library */
#include <stdlib.h>	/* required for random()*/
#include <time.h>	/* required for randomize() */

int trial(void);	/* function prototype */

void main(void)
	{
	int cars,goats;	/* counters for results */
	int i;

	cars=0; goats=0; /* not needed but to be clear */

	randomize();	/* init random seed to system clock */
	clrscr();	/* clear the screen */

	cputs("Goats Puzzle Simulation, 1000 trials\n\n\r");

	/* run simulation for 1000 trials */

	for (i=0; i<1000; i++)
	    if (trial() == 0) goats++; /* 0 = goat, 1 = car */
		else cars++;

	cprintf("Goats = %d  Cars = %d \n\r",goats,cars);

	getch();	/* wait for keypress */
	}

/* run one pass of puzzle */
/* return 1 if car, 0 if goat */

int trial(void)
	{
	/* each of these variables holds a door number 0-2 */

	int car,goat1,goat2;
	int choice,new_choice,open_door;

	/* set up doors and contents **/

	switch (random(6))	/* 6 possible permutations */
		{
		case 0: car = 0; goat1 = 1; goat2 = 2; break;
		case 1: car = 0; goat1 = 2; goat2 = 1; break;
		case 2: car = 1; goat1 = 0; goat2 = 2; break;
		case 3: car = 1; goat1 = 2; goat2 = 0; break;
		case 4: car = 2; goat1 = 0; goat2 = 1; break;
		case 5: car = 2; goat1 = 1; goat2 = 0; break;
		}

	/* select a door */

	choice = random(3);

	/* open a door with goat in it */

	if (choice == car)     	/* pick either goat door */
	    if (random(2) == 0)
		open_door = goat1;
	    else
		open_door = goat2;

	else     	 	/* pick other goat door */
	    if (choice == goat1)
		open_door = goat2;
	    else
		if (choice == goat2)	/* redundant but to be clear */
		    open_door = goat1;

	/* now change the choice to other door */

	if (choice == car)
	    {
	    if (open_door == goat1)
		new_choice = goat2;
	    if (open_door == goat2)	/* redundant code to be clear */
		new_choice = goat1;
	    }

	if (choice == goat1)
	    new_choice = car;		/* open door is goat 2 */

	if (choice == goat2)
	    new_choice = car; 		/* open door is goat 1 */

	/* and return the result */

	if (new_choice == car) return 1; 	/* car */
	else return 0;				/* goat */
	}

/* ----------------------------------------- */


Rate This Message: http://www.cryonet.org/cgi-bin/rate.cgi?msg=7072