Advertisement
Guest User

Monty Hall/Monty Fall

a guest
Dec 18th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 KB | None | 0 0
  1. from struct import unpack
  2.  
  3. r = open("/dev/urandom","rb")
  4.  
  5.  
  6. def randmod3():
  7.     rand = 0xFFFFFFFF
  8.     while rand == 0xFFFFFFFF:
  9.         rand = unpack("I",r.read(4))[0]
  10.     return rand%3
  11.  
  12. def randmod2():
  13.     rand = unpack("B",r.read(1))[0]
  14.     return rand%2
  15.  
  16. print "Monty Hall"
  17.  
  18. switch_win_count = 0
  19. stay_win_count = 0
  20.  
  21. for i in range(1000000):
  22.     doors = ["goat","goat","goat"]
  23.     doors[randmod3()] = "car"
  24.     choice = randmod3()
  25.     opened = -1
  26.     switch_choice = -1
  27.     possible_doors = {
  28.             0 : [1,2],
  29.             1 : [2,0],
  30.             2 : [0,1]
  31.             }
  32.     remaining_doors = possible_doors.get(choice);
  33.     if doors[choice] != "car":
  34.         if doors[remaining_doors[0]] == "car":
  35.             switch_choice = remaining_doors[0]
  36.             opened = remaining_doors[1]
  37.         else:
  38.             switch_choice = remaining_doors[1]
  39.             opened = remaining_doors[0]
  40.     else:
  41.         rand_door = randmod2()
  42.         opened = remaining_doors[rand_door]
  43.         switch_choice = remaining_doors[(rand_door+1)%2]
  44.    
  45.     if doors[switch_choice] == "car":
  46.         switch_win_count += 1
  47.  
  48.     if doors[choice] == "car":
  49.         stay_win_count += 1
  50.  
  51. print "switch wins: %d" % switch_win_count
  52. print "stay wins: %d" % stay_win_count
  53. print ""
  54. print "Monty Fall"
  55.  
  56. switch_win_count = 0
  57. stay_win_count = 0
  58.  
  59. for i in range(1000000):
  60.     doors = ["goat","goat","goat"]
  61.     doors[randmod3()] = "car"
  62.     choice = randmod3()
  63.     opened = -1
  64.     switch_choice = -1
  65.     possible_doors = {
  66.             0 : [1,2],
  67.             1 : [2,0],
  68.             2 : [0,1]
  69.             }
  70.     remaining_doors = possible_doors.get(choice);
  71.    
  72.     rand_door = randmod2()
  73.     opened = remaining_doors[rand_door]
  74.     switch_choice = remaining_doors[(rand_door+1)%2]
  75.  
  76.     if doors[switch_choice] == "car":
  77.         switch_win_count += 1
  78.  
  79.     if doors[choice] == "car":
  80.         stay_win_count += 1
  81.  
  82. print "switch wins: %d" % switch_win_count
  83. print "stay wins: %d" % stay_win_count
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement