__author__ = 'umesh.p.nair@gmail.com (Umesh Nair)'
__date__ = '2009/03/18'
import random
class GirlLionSimulator:
house = random.Random()
door = random.Random()
nTotal = 0
nFirstGirl = 0 nSecondGirl = 0 nThirdGirl = 0
lion = 0
firstHouse = 0 secondHouse = 0 thirdHouse = 0
lionThirdHouseFirstTrial = 0 lionThirdHouseSecondTrial = 0 lionThirdHouseThirdTrial = 0
def nextHouse(self):
return self.house.randint(1, 3)
def nextDoor(self):
return self.door.randint(1, 2)
def doOneExperiment(self):
self.nTotal += 1
houseChosen = self.nextHouse()
if houseChosen == 1: self.firstHouse += 1
return
elif houseChosen == 2: self.secondHouse += 1
else : self.thirdHouse += 1
firstDoor = self.nextDoor()
if firstDoor == 1: self.lionThirdHouseFirstTrial += 1
return
else: self.nFirstGirl += 1
secondDoor = self.nextDoor()
if secondDoor == 1: self.lionThirdHouseSecondTrial += 1
return
else: self.nSecondGirl += 1
thirdDoor = self.nextDoor()
if thirdDoor == 1: self.lionThirdHouseThirdTrial += 1
return
else: self.nThirdGirl += 1
def printNumberAndPercentage(self, title, n):
print title, "=", n, "=", n * 100.0 / self.nTotal, "%"
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)
if __name__ == '__main__':
GirlLionSimulator().doNTimes(1000000)