

The Problem

 100 prisoners are individually numbered 1 to 100
 A room having a cupboard of 100 opaque drawers numbered 1 to 100, that cannot be seen from outside.
 Cards numbered 1 to 100 are placed randomly, one to a drawer, and the drawers all closed; at the start.
 Prisoners start outside the room
 They can decide some strategy before any enter the room.
 Prisoners enter the room one by one, can open a drawer, inspect the card number in the drawer, then close the drawer.
 A prisoner can open no more than 50 drawers.
 A prisoner tries to find his own number.
 A prisoner finding his own number is then held apart from the others.
 If all 100 prisoners find their own numbers then they will all be pardoned. If any don't then all sentences stand. 


The task

 Simulate several thousand instances of the game where the prisoners randomly open drawers
 Simulate several thousand instances of the game where the prisoners use the optimal strategy mentioned in the Wikipedia article, of:
 First opening the drawer whose outside number is his prisoner number.
 If the card within has his number then he succeeds otherwise he opens the drawer with the same number as that of the revealed card. (until he opens his maximum).


Show and compare the computed probabilities of success for the two strategies, here, on this page.




References

 The unbelievable solution to the 100 prisoner puzzle standupmaths (Video).
 wp:100 prisoners problem
 100 Prisoners Escape Puzzle DataGenetics.
 Random permutation statistics#One hundred prisoners on Wikipedia.


 
10 rem 100 prisoners
20 rem set arrays
30 rem dr = drawers containing card values
40 rem ig = a list of numbers 1 through 100, shuffled to become the 
41 rem guess sequence for each inmate - method 1
50 dim dr(100),ig(100)
55 rem initialize drawers with own card in each drawer
60 for i=1 to 100:dr(i)=i:next 
 
1000 print chr$(147);"how many trials for each method";:input tt
1010 for m=1 to 2:su(m)=0:fa(m)=0
1015 for tn=1 to tt
1020 on m gosub 2000,3000
1025 rem ip = number of inmates who passed
1030 if ip=100 then su(m)=su(m)+1
1040 if ip<100 then fa(m)=fa(m)+1
1045 next tn
1055 next m
 
1060 print chr$(147);"Results:":print
1070 print "Out of";tt;"trials, the results are"
1071 print "as follows...":print
1072 print "1. Random Guessing:"
1073 print "  ";su(1);"successes"
1074 print "  ";fa(1);"failures"
1075 print "  ";su(1)/tn;"{left-crsr}% success rate.":print
1077 print "2. Chained Number Picking:"
1078 print "  ";su(2);"successes"
1079 print "  ";fa(2);"failures"
1080 print "  ";(su(2)/tn)*100;"{left-crsr}% success rate.":print
1100 print:print "Again?"
1110 get k$:if k$="" then 1110
1120 if k$="y" then 1000
1500 end
 
2000 rem random guessing method
2005 for x=1 to 100:ig(x)=x:next:ip=0:gosub 4000
2007 for i=1 to 100
2010 for x=1 to 100:t=ig(x):np=int(rnd(1)*100)+1:ig(x)=ig(np):ig(np)=t:next
2015 for g=1 to 50
2020 if dr(ig(g))=i then ip=ip+1:next i:return
2025 next g
2030 return
 
3000 rem chained method
3005 ip=0:gosub 4000
3007 rem iterate through each inmate
3010 fori=1to100
3015 ng=i:forg=1to50
3020 cd=dr(ng)
3025 ifcd=ithenip=ip+1:nexti:return
3030 ifcd<>ithenng=cd
3035 nextg:return
 
4000 rem shuffle the drawer cards randomly
4010 x=rnd(-ti)
4020 for i=1 to 100
4030 r=int(rnd(1)*100)+1:t=dr(i):dr(i)=dr(r):dr(r)=t:next
4040 return
 