100 prisoners: Difference between revisions

Line 6,446:
Random play success rate: 0.00000000000%</pre>
 
=={{header|VBScript}}==
<lang VB>
option explicit
const npris=100
const ntries=50
const ntests=1000.
dim drawer(100),opened(100),i
for i=1 to npris: drawer(i)=i:next
shuffle drawer
wscript.echo rf(tests(false)/ntests*100,10," ") &" % success for random"
wscript.echo rf(tests(true) /ntests*100,10," ") &" % success for optimal strategy"
 
function rf(v,n,s) rf=right(string(n,s)& v,n):end function
 
sub shuffle(d) 'knut's shuffle
dim i,j,t
randomize timer
for i=1 to npris
j=int(rnd()*i+1)
t=d(i):d(i)=d(j):d(j)=t
next
end sub
 
function tests(strat)
dim cntp,i,j
tests=0
for i=1 to ntests
shuffle drawer
cntp=0
if strat then
for j=1 to npris
if trystrat(j) then cntp=cntp+1
next
else
for j=1 to npris
if tryrand(j) then cntp=cntp+1
next
end if
if cntp=npris then tests=tests+1
next
end function
function tryrand(pris)
dim i,r
erase opened
for i=1 to ntries
do
r=int(rnd*npris+1)
loop until opened(r)=false
opened(r)=true
if drawer(r)= pris then tryrand=true : exit function
next
tryrand=false
end function
 
function trystrat(pris)
dim i,r
r=pris
for i=1 to ntries
if drawer(r)= pris then trystrat=true :exit function
r=drawer(r)
next
trystrat=false
end function </lang>
Output:
<pre>
0 % success for random
32.9 % success for optimal strategy
</pre>
=={{header|Wren}}==
{{trans|Go}}