#!/usr/bin/python # A simulation for a probability puzzle involving doors, girls and lions. __author__ = 'umesh.p.nair@gmail.com (Umesh Nair)' __date__ = '2009/03/18' import random class GirlLionSimulator: house = random.Random() door = random.Random() nTotal = 0 # Total number of trials nFirstGirl = 0 # Number of times we get a G* nSecondGirl = 0 # Number of times we get GG* nThirdGirl = 0 # Number of times we get GGG* lion = 0 # Number of times a lion is finally got firstHouse = 0 # Number of times the LL house is chosen secondHouse = 0 # Number of times the GG house is chosen thirdHouse = 0 # Number of times the LG house is chosen lionThirdHouseFirstTrial = 0 # Number of times when we get a lion in the first trial in the third house lionThirdHouseSecondTrial = 0 # Number of times when we get a girl in the first trial and lion in the second lionThirdHouseThirdTrial = 0 # Number of times when we get a girl in the first two trials and lion in the third # Randomly (1-3) select the next house. # 1 = Lion/Lion, 2 = Girl/Girl, 3 = Lion/Girl def nextHouse(self): return self.house.randint(1, 3) # Randomly (1-2) select the next door. # 1 = Lion, 2 = Girl def nextDoor(self): return self.door.randint(1, 2) # Do the whole experiment (select house, select door three times) one time. def doOneExperiment(self): self.nTotal += 1 houseChosen = self.nextHouse() if houseChosen == 1: # Lion/Lion House self.firstHouse += 1 return elif houseChosen == 2: # Girl/Girl House self.secondHouse += 1 else : # Lion/Girl house self.thirdHouse += 1 firstDoor = self.nextDoor() if firstDoor == 1: # Lion in the first door self.lionThirdHouseFirstTrial += 1 return else: # Girl in the first door self.nFirstGirl += 1 secondDoor = self.nextDoor() if secondDoor == 1: # Lion in the second door self.lionThirdHouseSecondTrial += 1 return else: # Girl in the second door self.nSecondGirl += 1 thirdDoor = self.nextDoor() if thirdDoor == 1: # Lion in the third door self.lionThirdHouseThirdTrial += 1 return else: # Girl in the third door self.nThirdGirl += 1 # Print the number of trials favored and the probability of one event. def printNumberAndPercentage(self, title, n): print title, "=", n, "=", n * 100.0 / self.nTotal, "%" # Do the experiment a specified number of times. def doNTimes(self, n): for i in range(0, n): self.doOneExperiment(); print "Total trials = ", self.nTotal print "" self.printNumberAndPercentage("First House", self.firstHouse) self.printNumberAndPercentage("Second House", self.secondHouse) self.printNumberAndPercentage("Third House", self.thirdHouse) print "" self.printNumberAndPercentage("Lion in First house", self.firstHouse) self.printNumberAndPercentage("Lion in Third house", self.lionThirdHouseFirstTrial) self.printNumberAndPercentage("Girl and Lion in Third house", self.lionThirdHouseSecondTrial) self.printNumberAndPercentage("Girl, Girl and Lion in Third house", self.lionThirdHouseThirdTrial) print "" self.printNumberAndPercentage("Lion in one trial", self.firstHouse + self.lionThirdHouseFirstTrial) self.printNumberAndPercentage("Lion in two trials", self.firstHouse + self.lionThirdHouseSecondTrial) self.printNumberAndPercentage("Lion in three trials", self.firstHouse + self.lionThirdHouseThirdTrial) print "" self.printNumberAndPercentage("Lion in one trial", self.firstHouse + self.lionThirdHouseFirstTrial) self.printNumberAndPercentage("Lion in one or two trials", self.firstHouse + self.lionThirdHouseFirstTrial + self.lionThirdHouseSecondTrial) self.printNumberAndPercentage("Lion in one, two or three trials", self.firstHouse + self.lionThirdHouseFirstTrial + self.lionThirdHouseSecondTrial + self.lionThirdHouseThirdTrial) print "" self.printNumberAndPercentage("Girls in second house", self.secondHouse) self.printNumberAndPercentage("Girl in third house", self.nFirstGirl) self.printNumberAndPercentage("Girl and Girl in third house", self.nSecondGirl) self.printNumberAndPercentage("Girl, Girl and Girl in third house", self.nThirdGirl) print "" self.printNumberAndPercentage("Girl in at least one trial", self.secondHouse + self.nFirstGirl) self.printNumberAndPercentage("Girls in at least two trials", self.secondHouse + self.nSecondGirl) self.printNumberAndPercentage("Girls in three trials", self.secondHouse + self.nThirdGirl) # Main standalone program if __name__ == '__main__': GirlLionSimulator().doNTimes(1000000)