logic - Monty Hall simulation issue regarding arrays -


i running issue simulation did of "montey hall" statistics riddle. should consistent results answer (33%/66%), every third time run simulation results end being 0/100 , flip to 66%/33%. believe issue might how creating arrays (i.e results of 1 simulation bleeding on next simulation), can't pinpoint issue. also, if have tips on better way write simulation, appreciate well.

below code

#simulates guessing door def sim_guess(nsim):     answer = []     guess = [0,1,2]     stratagy = [0.2,0.6,0.2]     element in range(nsim):         answer.append(np.random.choice(guess, p=stratagy))     return answer  #simulates placing prize def simulate_prizedoor(nsim):     doors = [0,1,2]     answer = []     element in range(nsim):         answer.append(np.random.choice(doors))     return answer  #simulates opening door reveal goat def goat_door(prize, guess):     answer = []     in range(len(prize)):         door = [0,1,2]         if prize[i] == guess[i]:             door.remove(prize[i])             answer.append(np.random.choice(door))         else:             door.remove(prize[i])             door.remove(guess[i])             answer.append(door[0])     return answer  #simulates changing guess after goat has been revealed def switch_guess(goat, guess):     answer = []     in range(len(goat)):         door = [0,1,2]         door.remove(goat[i])         door.remove(guess[i])         answer.append(door[0])     return answer #shows percentages after 10,000 trials def win_percentage(prize, guess):     wins = []     element in prize:         wins.append(prize[element] == guess[element])      answer = (float(np.sum(wins))/len(guess))*100     return answer  prize = simulate_prizedoor(10000) guess = sim_guess(10000)  #wins without changing guess print win_percentage(prize, guess)  #wins changing guess goat = goat_door(prize, guess) switch = switch_guess(goat, guess) print win_percentage(prize, switch) 

i feel lot easier objects, each game separate others.

import random  class game:         winningdoor = 0         chosendoor = 0         goat = 0         def __init__(self):                 self.winningdoor = random.randint(1,3)         def play(self, move, willswap):                 self.chosendoor = move                 self.goat = 0                 i=1                 while(self.goat <= 0):                         if(i != self.winningdoor , != self.chosendoor):                                 self.goat =                         += 1                 if(willswap):                         self.chosendoor = 6-(self.chosendoor + self.goat)                 return (self.winningdoor == self.chosendoor)   def main():         swapwins = 0         staywins = 0         in range(0,10000):                 testswap = game()                 if(testswap.play(random.randint(1,3), 1)):                         swapwins += 1                 teststay = game()                 if(teststay.play(random.randint(1,3), 0)):                         staywins += 1         print swapwins, staywins  main() 

here, each game made separately, each game being played once. it's not efficient use of objects, , subroutine this, if want more statistics, end being better. potentially confusing thing here

self.chosendoor = 6-(self.chosendoor + self.goat) 

which simplification of statement if goat 1 , 2 chosen, change 3; if goat 2 , 3 chosen, change 1; if goat 3 , chosen 1, change 2; etc... far why original code didn't work, returning of things in groups of 10000 looks odd , difficult debug. random numbers accomplished randint, make code more human-readable.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -