

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.


0 GOTO 9
 
1 FOR X = 0 TO N:J(X) = X: NEXT: FOR I = 0 TO N:FOR X = 0 TO N:T = J(X):NP =  INT ( RND (1) * H):J(X) = J(NP):J(NP) = T: NEXT :FOR G = 1 TO W:IF D(J(G)) = I THEN IP = IP + 1: NEXT I: RETURN 
2 NEXT G:RETURN 
 
3 FOR I = 0 TO N:NG = I: FOR G = 0 TO W:CD = D(NG):IF CD = I THEN IP = IP + 1: NEXT I: RETURN 
4 NG = CD:IF CD = I THEN STOP
5 NEXT G: RETURN 
 
9 H=100:N=H-1:DIM D(99),J(99):FOR I = 0 TO N:D(I) = I: NEXT:W=INT(H/2)-1:M$=CHR$(13):M$(1)="RANDOM GUESSING":M$(2)="CHAINED NUMBER PICKING"
 
1000 FOR Q = 0 TO 1 STEP 0 : HOME : PRINT "100 PRISONERS"M$: INPUT "HOW MANY TRIALS FOR EACH METHOD? ";  TT
1010     VTAB 2:CALL-958:PRINT M$"RESULTS:"M$
1020     FOR M = 1 TO 2: SU(M) = 0:FA(M) = 0
1030         FOR TN = 1 TO TT
1040             VTAB 4:PRINT M$ "   OUT OF " TT " TRIALS, THE RESULTS ARE"M$"   AS FOLLOWS...";
1050             IP = 0: X =  RND ( - TI): FOR I = 0 TO N:R =  INT ( RND (1) * N):T = D(I):D(I) = D(R):D(R) = T: NEXT
1060             ON M GOSUB 1,3 : SU(M) = SU(M) + (IP = H):FA(M) = FA(M) + (IP < H)
1070             FOR Z = 1 TO 2
1071                 PRINT M$M$Z". "M$(Z)":"M$
1073                 PRINT "   "SU(Z)" SUCCESSES"TAB(21)
1074                 PRINT "   "FA(Z)" FAILURES"M$
1075                 PRINT "   "(SU(Z) / TT) * 100"% SUCCESS RATE.";:CALL-868
1090     NEXT Z,TN,M
 
1100     PRINT M$M$"AGAIN?"
1110     GET K$
1120     Q = K$ <> "Y" AND K$ <> CHR$(ASC("Y") + 32) : NEXT Q
 