RosettaCodeData/Task/100-prisoners/PureBasic/100-prisoners.basic

38 lines
928 B
Plaintext

#PRISONERS=100
#DRAWERS =100
#LOOPS = 50
#MAXPROBE = 10000
OpenConsole()
Dim p1(#PRISONERS,#DRAWERS)
Dim p2(#PRISONERS,#DRAWERS)
Dim d(#DRAWERS)
For i=1 To #DRAWERS : d(i)=i : Next
Start:
For probe=1 To #MAXPROBE
RandomizeArray(d(),1,100)
c1=0 : c2=0
For m=1 To #PRISONERS
p2(m,1)=d(m) : If d(m)=m : p2(m,0)=1 : EndIf
For n=1 To #LOOPS
p1(m,n)=d(Random(100,1))
If p1(m,n)=m : p1(m,0)=1 : EndIf
If n>1 : p2(m,n)=d(p2(m,n-1)) : If p2(m,n)=m : p2(m,0)=1 : EndIf : EndIf
Next n
Next m
For m=1 To #PRISONERS
If p1(m,0) : c1+1 : p1(m,0)=0 : EndIf
If p2(m,0) : c2+1 : p2(m,0)=0 : EndIf
Next m
If c1=#PRISONERS : w1+1 : EndIf
If c2=#PRISONERS : w2+1 : EndIf
Next probe
Print("TRIALS: "+Str(#MAXPROBE))
Print(" RANDOM= "+StrF(100*w1/#MAXPROBE,2)+"% STATEGY= "+StrF(100*w2/#MAXPROBE,2)+"%")
PrintN(~"\tFIN =q.") : inp$=Input()
w1=0 : w2=0
If inp$<>"q" : Goto Start : EndIf