Jump to content

100 prisoners: Difference between revisions

Added XPL0 example.
(Added XPL0 example.)
Line 6,521:
 
strategy = optimal pardoned = 31,139 relative frequency = 31.14%
</pre>
 
=={{header|XPL0}}==
<lang XPL0>int Drawer(100);
 
proc KShuffle; \Randomly rearrange the cards in the drawers
\(Woe unto thee if Stattolo shuffle is used instead of Knuth shuffle.)
int I, J, T;
[for I:= 100-1 downto 1 do
[J:= Ran(I+1); \range [0..I-1]
T:= Drawer(I); Drawer(I):= Drawer(J); Drawer(J):= T;
];
];
 
func Stategy2; \Return 'true' if stragegy succeeds
int Prisoner, Card, Try;
[for Prisoner:= 1 to 100 do
[Card:= Drawer(Prisoner-1);
Try:= 1;
loop [if Card = Prisoner then quit;
if Try >= 50 then return false;
Card:= Drawer(Card-1);
Try:= Try+1;
];
];
return true;
];
 
func Stategy1; \Return 'true' if stragegy succeeds
int Prisoner, I, D(100);
[for Prisoner:= 1 to 100 do
loop [for I:= 0 to 100-1 do D(I):= I+1;
KShuffle;
for I:= 1 to 50 do
if Drawer(D(I-1)) = Prisoner then quit;
return false;
];
return true;
];
 
proc Strategy(S);
int S, I, Sample;
real Successes;
[Successes:= 0.;
for Sample:= 1 to 100_000 do
[for I:= 0 to 100-1 do Drawer(I):= I+1;
KShuffle;
case S of
1: if Stategy1 then Successes:= Successes + 1.;
2: if Stategy2 then Successes:= Successes + 1.
other [];
];
RlOut(0, Successes/100_000.*100.); Text(0, "%^m^j");
];
 
[Format(3, 12);
Text(0, "Random strategy success rate: ");
Strategy(1);
Text(0, "Optimal strategy success rate: ");
Strategy(2);
]</lang>
 
{{out}}
<pre>
Random strategy success rate: 0.000000000000%
Optimal strategy success rate: 31.085000000000%
</pre>
 
772

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.