import random, sys TRIALS = 1000 # trials per strategy (swap and don't swap) DOORS = 3 # number of doors in the experiment def test_swap(doors): door_list = [0]*(doors - 1) + [1] random.shuffle(door_list) rand_doors = dict(zip(range(doors), door_list)) # create the doors door_num = random.choice(list(rand_doors.keys())) # choose a door if rand_doors[door_num]: # if you chose the prize door # You lose the prize because the host eliminates all but one of the # goat doors, and you swap from the prize door to that goat door return False elif not rand_doors[door_num]: # if you chose a goat door # You win the prize because the host eliminates all of the other # goat doors, and you swap from your goat door to the prize door return True def test_no_swap(doors): door_list = [0]*(doors - 1) + [1] random.shuffle(door_list) rand_doors = dict(zip(range(doors), door_list)) # create the doors door_num = random.choice(list(rand_doors.keys())) # choose a door if rand_doors[door_num]: # if you chose the prize door # You win the prize because the host eliminates all but one # of the other goat doors, but you keep the prize door return True elif not rand_doors[door_num]: # if you chose a goat door # You lose the prize because the host eliminates all the # other goat doors, but you keep your original goat door return False ################################################################## ### Main Script ### ################################################################## if sys.version_info[0] == 2: print 'Running ...', elif sys.version_info[0] == 3: exec("print('Running...', end=' ')") swap_wins, no_swap_wins = 0, 0 for i in range(TRIALS): if test_swap(DOORS): swap_wins += 1 if test_no_swap(DOORS): no_swap_wins += 1 print('Done') print('Not swapping doors won you {0} prizes out of {1} ({2}%)'.format(no_swap_wins, TRIALS, str(float(no_swap_wins) / TRIALS * 100)[:5])) print('Swapping doors won you {0} prizes out of {1} ({2}%)'.format(swap_wins, TRIALS, str(float(swap_wins) / TRIALS * 100)[:5]))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question