Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
58
Task/100-prisoners/XPL0/100-prisoners.xpl0
Normal file
58
Task/100-prisoners/XPL0/100-prisoners.xpl0
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
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]
|
||||
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);
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue