Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from struct import unpack
- r = open("/dev/urandom","rb")
- def randmod3():
- rand = 0xFFFFFFFF
- while rand == 0xFFFFFFFF:
- rand = unpack("I",r.read(4))[0]
- return rand%3
- def randmod2():
- rand = unpack("B",r.read(1))[0]
- return rand%2
- print "Monty Hall"
- switch_win_count = 0
- stay_win_count = 0
- for i in range(1000000):
- doors = ["goat","goat","goat"]
- doors[randmod3()] = "car"
- choice = randmod3()
- opened = -1
- switch_choice = -1
- possible_doors = {
- 0 : [1,2],
- 1 : [2,0],
- 2 : [0,1]
- }
- remaining_doors = possible_doors.get(choice);
- if doors[choice] != "car":
- if doors[remaining_doors[0]] == "car":
- switch_choice = remaining_doors[0]
- opened = remaining_doors[1]
- else:
- switch_choice = remaining_doors[1]
- opened = remaining_doors[0]
- else:
- rand_door = randmod2()
- opened = remaining_doors[rand_door]
- switch_choice = remaining_doors[(rand_door+1)%2]
- if doors[switch_choice] == "car":
- switch_win_count += 1
- if doors[choice] == "car":
- stay_win_count += 1
- print "switch wins: %d" % switch_win_count
- print "stay wins: %d" % stay_win_count
- print ""
- print "Monty Fall"
- switch_win_count = 0
- stay_win_count = 0
- for i in range(1000000):
- doors = ["goat","goat","goat"]
- doors[randmod3()] = "car"
- choice = randmod3()
- opened = -1
- switch_choice = -1
- possible_doors = {
- 0 : [1,2],
- 1 : [2,0],
- 2 : [0,1]
- }
- remaining_doors = possible_doors.get(choice);
- rand_door = randmod2()
- opened = remaining_doors[rand_door]
- switch_choice = remaining_doors[(rand_door+1)%2]
- if doors[switch_choice] == "car":
- switch_win_count += 1
- if doors[choice] == "car":
- stay_win_count += 1
- print "switch wins: %d" % switch_win_count
- print "stay wins: %d" % stay_win_count
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement