Monty hall simulation returning 50% odds?

微笑、不失礼 提交于 2019-12-13 00:26:39

问题


from random import randint

numberOfDoors = 3

success = 0
attempts = 0

while True:
    try:
        doors = [0] * numberOfDoors
        doors[randint(0, numberOfDoors - 1)] = 1

        chosen = randint(0, numberOfDoors - 1)

        while numberOfDoors > 2:
            notIn = -1
            while notIn == -1:
                index = randint(0, numberOfDoors - 1)
                if doors[index] == 0 and index != chosen:
                    notIn = index

            if notIn < chosen:
                chosen -= 1
            del doors[notIn]
            numberOfDoors -= 1

        # doors is 2, so not chosen (0 or 1) will return the opposite (1 or 0)
        success += doors[not chosen]
        attempts += 1
        if attempts % 1000000 == 0:
            print float(success) / float(attempts)
    except KeyboardInterrupt:
        print float(success) / float(attempts)
        break

My results are almost exactly 50% after a few hours of simulation - am I doing something specifically wrong?

Theoretically the door you choose is between 1/3 odds and 2/3 odds, so you should get higher than 50 at the very least.

This answer seems to do the same thing as me (ignoring that he doesn't do anything about monty's choice - I wanted to illustrate the concept).


回答1:


You're forgetting to reset numberOfDoors (number of doors still closed, right?) back to 3. Since every iteration of the first while True: represents a new game show run, the show starts with all three doors initially closed.

...
while True:
    numberOfDoors = 3
    try:
        doors = [0] * numberOfDoors
        doors[randint(0, numberOfDoors - 1)] = 1
...

Next time, try adding print statements to help you debug. In this case, adding print doors right after you assign a car shows that doors has only two elements after the first iteration.




回答2:


I wrote a Monty Hall simulation problem myself a while ago. Maybe it will help you with your code - in particular the comments may be useful:

from __future__ import division
import random

results = [] # a list containing the results of the simulations, either 'w' or 'l', win or lose

count = 0

while count <200: #number of simulations to perform
    l = [] 

    prize = random.randint(1, 3) #choose the prize door

    initialchoice = random.randint(1, 3) #make an initial choice (the door the contestant chooses)

    exposed = random.randint(1, 3) #choose the exposed door (the door the host chooses)

    while exposed == initialchoice or exposed == prize: #make sure exposed is not same as prize or the initial choice
        exposed = random.randint(1, 3)

    if initialchoice != prize:
        results.append('l') #if the initial choice was incorrect, append 'l'
    else:
        results.append('w') #if the initial choice was correct, append 'w'

    count += 1
    print 'prize door:', prize, 'initial choice:',initialchoice, 'exposed door:',exposed #print the results of the simulation
    print

w = 0
for i in results:
    if i == 'w':
        w += 1
print w/len(results) #fraction of times sticking with the original door was successful


来源:https://stackoverflow.com/questions/23820487/monty-hall-simulation-returning-50-odds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!