

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.


NumOfTrials := 20000
randomFailTotal := 0, strategyFailTotal := 0
prisoners := [], drawers := [], Cards := []
loop, 100
	prisoners[A_Index] := A_Index				; create prisoners
	, drawers[A_Index] := true				; create drawers
 
loop, % NumOfTrials
{
	loop, 100
		Cards[A_Index] := A_Index			; create cards for this iteration
	loop, 100
	{
		Random, rnd, 1, Cards.count()
		drawers[A_Index] := Cards.RemoveAt(rnd)		; randomly place cards in drawers
	}
	;-------------------------------------------
	; randomly open drawers
	RandomList := []
	loop, 100
		RandomList[A_Index] := A_Index
	Fail := false
	while (A_Index <=100) && !Fail
	{
		thisPrisoner := A_Index
		res := ""
		while (thisCard <> thisPrisoner) && !Fail
		{
			Random, rnd, 1, % RandomList.Count()	; choose random number
			NextDrawer := RandomList.RemoveAt(rnd)	; remove drawer from random list (don't choose more than once)
			thisCard := drawers[NextDrawer]		; get card from this drawer
			if (A_Index > 50)
				Fail := true
		}
		if Fail
			randomFailTotal++
	}
	;-------------------------------------------
	; use optimal strategy
	Fail := false
	while (A_Index <=100) && !Fail
	{
		counter := 1, thisPrisoner := A_Index
		NextDrawer := drawers[thisPrisoner]		; 1st trial, drawer whose outside number is prisoner number
		while (drawers[NextDrawer] <> thisPrisoner) && !Fail
		{	
			NextDrawer := drawers[NextDrawer]	; drawer with the same number as that of the revealed card
			if ++counter > 50
				Fail := true
		}
		if Fail
			strategyFailTotal++
	}
}
MsgBox %  "Number Of Trials = " NumOfTrials
		. "`nOptimal Strategy:`t" (1 - strategyFailTotal/NumOfTrials) *100 " % success rate" 
		. "`nRandom Trials:`t" (1 - randomFailTotal/NumOfTrials) *100 " % success rate"