RosettaCodeData/Task/100-prisoners/Commodore-BASIC/100-prisoners.basic

60 lines
1.7 KiB
Plaintext

10 rem 100 prisoners
20 rem set arrays
30 rem dr = drawers containing card values
40 rem ig = a list of numbers 1 through 100, shuffled to become the
41 rem guess sequence for each inmate - method 1
50 dim dr(100),ig(100)
55 rem initialize drawers with own card in each drawer
60 for i=1 to 100:dr(i)=i:next
1000 print chr$(147);"how many trials for each method";:input tt
1010 for m=1 to 2:su(m)=0:fa(m)=0
1015 for tn=1 to tt
1020 on m gosub 2000,3000
1025 rem ip = number of inmates who passed
1030 if ip=100 then su(m)=su(m)+1
1040 if ip<100 then fa(m)=fa(m)+1
1045 next tn
1055 next m
1060 print chr$(147);"Results:":print
1070 print "Out of";tt;"trials, the results are"
1071 print "as follows...":print
1072 print "1. Random Guessing:"
1073 print " ";su(1);"successes"
1074 print " ";fa(1);"failures"
1075 print " ";su(1)/tn;"{left-crsr}% success rate.":print
1077 print "2. Chained Number Picking:"
1078 print " ";su(2);"successes"
1079 print " ";fa(2);"failures"
1080 print " ";(su(2)/tn)*100;"{left-crsr}% success rate.":print
1100 print:print "Again?"
1110 get k$:if k$="" then 1110
1120 if k$="y" then 1000
1500 end
2000 rem random guessing method
2005 for x=1 to 100:ig(x)=x:next:ip=0:gosub 4000
2007 for i=1 to 100
2010 for x=1 to 100:t=ig(x):np=int(rnd(1)*100)+1:ig(x)=ig(np):ig(np)=t:next
2015 for g=1 to 50
2020 if dr(ig(g))=i then ip=ip+1:next i:return
2025 next g
2030 return
3000 rem chained method
3005 ip=0:gosub 4000
3007 rem iterate through each inmate
3010 fori=1to100
3015 ng=i:forg=1to50
3020 cd=dr(ng)
3025 ifcd=ithenip=ip+1:nexti:return
3030 ifcd<>ithenng=cd
3035 nextg:return
4000 rem shuffle the drawer cards randomly
4010 x=rnd(-ti)
4020 for i=1 to 100
4030 r=int(rnd(1)*100)+1:t=dr(i):dr(i)=dr(r):dr(r)=t:next
4040 return