100 Prisoners Dilemma – Code Always Resulting in Failure (all prisoners die)

I want to see if the strategy of making each prisoner first pick the box with his own number on it and then open the box bearing the number contained in the previous one (and so forth) actually results in success 31% of the time. Thus, I wanted to recreate the strategy in Python and iterate it 1000 times. (Note that I’m storing the outcomes of each trial in a list to then be summed, although I’m not summing it here yet).

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import random
boxesnum = list(range(100)) #generate list of 100 boxes
outcomes = [] #create empty list to store outcome of each iteration
for i in range(1000): #iterate process 1000 times
random1 = list(range(100))
random.shuffle(random1) #generate list of numbers from 0 to 99 and shuffle them randomly
boxes = dict(zip(boxesnum,random1)) #create dictionary mapping each box to a number randomly shuffled
lastprisoner = 0 #initialize count of prisoners who successfully find their number in the boxes
for prisoner in boxesnum:
boxopen = prisoner #let prisoner start "opening" boxes from the one with his own number (prisoners numbered from 0 to 99)
boxesopened = 0 #initialize count for number of boxes opened by each prisoner
while boxesopened < 50: #let prisoner open up to 50 boxes
if boxopen == random1[boxopen]: #if the prisoner opens a box with his own number, skip to next prisoner and add 1 to count of successful prisoners
lastprisoner += 1
break
else:
boxopen = random1[boxopen] #next box the prisoner opens is box #(number contained in previous box)
boxesopened += 1 #update count of boxes opened by the prisoner
else:
print("All prisoners are dead!") #if prisoner has to open more than 50 boxes, stop iteration and all prisoners die
outcomes.append(0) #add 0 to list of outcomes of each trial
break
if lastprisoner == 100:
print("Success!")
outcomes.append(1) #if all prisoners are successful, add 1 to list of outcomes of each trial
print(outcomes)
</code>
<code>import random boxesnum = list(range(100)) #generate list of 100 boxes outcomes = [] #create empty list to store outcome of each iteration for i in range(1000): #iterate process 1000 times random1 = list(range(100)) random.shuffle(random1) #generate list of numbers from 0 to 99 and shuffle them randomly boxes = dict(zip(boxesnum,random1)) #create dictionary mapping each box to a number randomly shuffled lastprisoner = 0 #initialize count of prisoners who successfully find their number in the boxes for prisoner in boxesnum: boxopen = prisoner #let prisoner start "opening" boxes from the one with his own number (prisoners numbered from 0 to 99) boxesopened = 0 #initialize count for number of boxes opened by each prisoner while boxesopened < 50: #let prisoner open up to 50 boxes if boxopen == random1[boxopen]: #if the prisoner opens a box with his own number, skip to next prisoner and add 1 to count of successful prisoners lastprisoner += 1 break else: boxopen = random1[boxopen] #next box the prisoner opens is box #(number contained in previous box) boxesopened += 1 #update count of boxes opened by the prisoner else: print("All prisoners are dead!") #if prisoner has to open more than 50 boxes, stop iteration and all prisoners die outcomes.append(0) #add 0 to list of outcomes of each trial break if lastprisoner == 100: print("Success!") outcomes.append(1) #if all prisoners are successful, add 1 to list of outcomes of each trial print(outcomes) </code>
import random

boxesnum = list(range(100))      #generate list of 100 boxes

outcomes = []           #create empty list to store outcome of each iteration

for i in range(1000):        #iterate process 1000 times
    
    random1 = list(range(100))
    random.shuffle(random1)       #generate list of numbers from 0 to 99 and shuffle them randomly

    boxes = dict(zip(boxesnum,random1))       #create dictionary mapping each box to a number randomly shuffled

    lastprisoner = 0       #initialize count of prisoners who successfully find their number in the boxes
    
    for prisoner in boxesnum:
        boxopen = prisoner      #let prisoner start "opening" boxes from the one with his own number (prisoners numbered from 0 to 99)
        boxesopened = 0         #initialize count for number of boxes opened by each prisoner
        
        while boxesopened < 50:     #let prisoner open up to 50 boxes
           
            if boxopen == random1[boxopen]:     #if the prisoner opens a box with his own number, skip to next prisoner and add 1 to count of successful prisoners
                lastprisoner += 1
                break
            else:
                boxopen = random1[boxopen]       #next box the prisoner opens is box #(number contained in previous box)
                boxesopened += 1           #update count of boxes opened by the prisoner
                
        else:
            print("All prisoners are dead!")      #if prisoner has to open more than 50 boxes, stop iteration and all prisoners die
            outcomes.append(0)          #add 0 to list of outcomes of each trial
            break
            
    if lastprisoner == 100:          
        print("Success!")
        outcomes.append(1)              #if all prisoners are successful, add 1 to list of outcomes of each trial
print(outcomes)

The problem is that it only returns failures (all prisoners die in each of the 100 trials), while the strategy implemented should have a success rate of 31%.

I really cannot understand why this code isn’t working.

Could there be a problem with the way the “shuffle” method is randomizing the content of the boxes?

Note: I’m fairly new to python and programming in general.

6

You checked that the box opened was the same box, not that it was the prisoner’s box. Change the line if boxopen == random1[boxopen]: to if prisoner == random1[boxopen]:.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật