100 prisoners: Difference between revisions

Content added Content deleted
(1-based index)
Line 2,007: Line 2,007:
See [[#Pascal]].
See [[#Pascal]].
=={{header|EasyLang}}==
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">for i range 100
<syntaxhighlight lang="easylang">for i = 1 to 100
drawer[] &= i
drawer[] &= i
sampler[] &= i
sampler[] &= i
Line 2,014: Line 2,014:
for i = len drawer[] downto 2
for i = len drawer[] downto 2
r = random i
r = random i
swap drawer[r] drawer[i - 1]
swap drawer[r] drawer[i]
.
.
.
.
subr play_random
subr play_random
call shuffle_drawer
call shuffle_drawer
found = 1
for prisoner = 1 to 100
prisoner = 0
while prisoner < 100 and found = 1
found = 0
found = 0
i = 0
for i = 1 to 50
while i < 50 and found = 0
r = random (100 - i)
r = random (100 - i)
card = drawer[sampler[r]]
card = drawer[sampler[r]]
Line 2,030: Line 2,027:
if card = prisoner
if card = prisoner
found = 1
found = 1
break 1
.
.
i += 1
.
.
prisoner += 1
if found = 0
break 1
.
.
.
.
.
subr play_optimal
subr play_optimal
call shuffle_drawer
call shuffle_drawer
found = 1
for prisoner = 1 to 100
prisoner = 0
while prisoner < 100 and found = 1
reveal = prisoner
reveal = prisoner
found = 0
found = 0
i = 0
for i = 1 to 50
while i < 50 and found = 0
card = drawer[reveal]
card = drawer[reveal]
if card = prisoner
if card = prisoner
found = 1
found = 1
break 1
.
.
reveal = card
reveal = card
i += 1
.
.
prisoner += 1
if found = 0
break 1
.
.
.
.
.
n = 10000
n = 10000
pardoned = 0
win = 0
for round range n
for _ = 1 to n
call play_random
call play_random
pardoned += found
win += found
.
.
print "random: " & 100.0 * pardoned / n & "%"
print "random: " & 100.0 * win / n & "%"
#
#
pardoned = 0
win = 0
for round range n
for _ = 1 to n
call play_optimal
call play_optimal
pardoned += found
win += found
.
.
print "optimal: " & 100.0 * pardoned / n & "%"</syntaxhighlight>
print "optimal: " & 100.0 * win / n & "%"</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>