33 lines
1.2 KiB
Text
33 lines
1.2 KiB
Text
// Rosetta Code problem: http://rosettacode.org/wiki/100_prisoners
|
|
// by Galileo, 05/2022
|
|
|
|
sub play(prisoners, iterations, optimal)
|
|
local prisoner, pardoned, found, drawer, drawers(prisoners), i, j, k, p, x
|
|
|
|
for i = 1 to prisoners : drawers(i) = i : next
|
|
|
|
for i = 1 to iterations
|
|
for k = 1 to prisoners : x = ran(prisoners) + 1 : p = drawers(x) : drawers(x) = drawers(k) : drawers(k) = p : next
|
|
for prisoner = 1 to prisoners
|
|
found = false
|
|
if optimal then drawer = prisoner else drawer = ran(prisoners) + 1 end if
|
|
for j = 1 to prisoners / 2
|
|
drawer = drawers(drawer)
|
|
if drawer = prisoner found = true : break
|
|
if not optimal drawer = ran(prisoners) + 1
|
|
next
|
|
if not found break
|
|
next
|
|
pardoned = pardoned + found
|
|
next
|
|
|
|
return 100 * pardoned / iterations
|
|
end sub
|
|
|
|
iterations = 10000
|
|
print "Simulation count: ", iterations
|
|
for prisoners = 10 to 100 step 90
|
|
random = play(prisoners, iterations, false)
|
|
optimal = play(prisoners, iterations, true)
|
|
print "Prisoners: ", prisoners, ", random: ", random, ", optimal: ", optimal
|
|
next
|