All pastes #419009 Raw Edit

Monty Hall simulator

public python v1 · immutable
#419009 ·published 2007-03-31 23:48 UTC
rendered paste body
#### A program to aid understanding of Bayes' theorem in the Monty Hall problem#### By BodaciousBrian##import randomclass door(): # doors are made into objects    def __init__(self, doorname):        self.isopen = False        self.contents = False        self.name = (doorname)        self.conpick = False        self.hostpick = False        self.notpick = False    def openit(self):        self.isopen = True    def reset(self):        self.isopen = False        self.contents = False        self.conpick = False        self.hostpick = False        self.notpick = False    def debug(self):        print "isopen:   %s" % (self.isopen)        print "contents  %s" % (self.contents)        print "name      %s" % (self.name)        print "firstpick %s" % (self.conpick)        print "hostpick  %s" % (self.hostpick)        print "lastpick  %s" % (self.switchpick)class contestant(): #contestant object    def __init__(self):        self.picked = ""    def reset(self):        self.picked = ""    def pick(self):        x = randomdoor()        x.conpick = True        x.isopen = True        self.picked = x    def switch(self):        self.picked = remaindoor()class host():  #this is the host object    def __init__(self):        self.picked = ""        self.didhepick = False    def reset(self):        self.picked = ""        self.didhepick = False    def pick(self, aemt):        if aemt == False:            x = randomdoornotcon()            x.isopen = True            self.picked = x        if aemt == True:            x = unpickedmtdoor()            x.isopen = True            self.picked = xdef resetdoors(): #resets all the doors back to defualts    a.reset()    b.reset()    c.reset()def resetplayers(): #resets players back to defualts    con.reset()    hos.reset()def resetall(): #resets the test back to starting point(for running multiple tests)    resetdoors()    resetplayers()def randomdoor(): #generates random door, a b or c    x = random.randint(1, 3)    if x == 1:        return a    if x == 2:        return b    if x== 3:        return cdef randomdoornotcon(): #pick random door that contestant didn't pick    while hos.didhepick == False:        x = random.randint(1, 3)        if x == 1:            if a.conpick == True:                continue            hos.didhepick = True            hos.picked = a            return a        if x == 2:            if b.conpick == True:                continue            hos.didhepick = True            hos.picked = b            return b        if x == 3:            if c.conpick == True:                continue            hos.didhepick = True            hos.picked = c            return cdef unpickedmtdoor():#finds the door that the contestant didn't pick, that is empty    for i in lis:        if i.conpick == False and i.contents == False:            hos.didhepick = True            hos.picked = i            return idef remaindoor(): #find the door that neither host, nor contestant picked    if a.isopen == False:        a.isopen = True        return a    if b.isopen == False:        b.isopen = True        return b    if c.isopen == False:        c.isopen == True        return c    else:        print "error with remaindoor!"def randomcontents(): #puts the prize behind a random door.    x = random.randint(1, 3)    if x == 1:        a.contents = True    if x == 2:        b.contents = True    if x== 3:        c.contents = True############################################ variables ############################################cycles = 1000  #number oif times to run the testswitch = False        #will our contestant switch?hswitch = True       #will the host always open an empty door? or a random one?##################################PROGRAM STARTa = door("a")b = door("b")c = door("c")lis = (a, b, c)con = contestant()hos = host()conright = 0hosright = 0counter = 0while counter < cycles:    counter += 1    randomcontents()    con.pick()    hos.pick(hswitch)    if hos.picked.contents == True:        hosright += 1        resetall()        continue    if switch == False:        if con.picked.contents == True:            conright += 1    if switch == True:        con.switch()        if con.picked.contents == True:            conright += 1    resetall()percentage = (1.00 * conright / cycles)print "total guesses: %s" % (cycles)print "total correct: %s" % (conright)print "contestant guessed right %s percent of the time" % (percentage)