import random, sys TRIALS = 1000 # trials per strategy (swap and don't swap) def test_swap(): door_list = [0, 0, 1] random.shuffle(door_list) rand_doors = dict(zip([0, 1, 2], 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 one of the goat doors, # and you swap from the prize door to the other goat door return False elif not rand_doors[door_num]: # if you chose a goat door # You win the prize because the host the other goat # door, and you swap from your goat door to the prize door return True def test_no_swap(): door_list = [0, 0, 1] random.shuffle(door_list) rand_doors = dict(zip([0, 1, 2], 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 one # of the 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 the # other goat door, 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(): swap_wins += 1 if test_no_swap(): no_swap_wins += 1 print('Done') print('Not swapping doors won you {} prizes out of {} ({}%)'.format(no_swap_wins, TRIALS, str(float(no_swap_wins) / TRIALS * 100)[:5])) print('Swapping doors won you {} prizes out of {} ({}%)'.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