/* truel.tc - tct - 8/18/9 - linux version only /* a "truel" is a 3-way duel - please see http://www.yofx.org/?p=194 for /* background on this interesting problem - in this case, Red=Zee, /* Yellow=Toby (can't see a Black cat) and Blue=Luna /* z vs t duel hit probabilities z(0)/z(1) and t(0)/t(1) /* returns 1 if z wins else returns 0 duel int z(1),t(1) [ if (random(1,z(1))<=z(0)) return 1 if !(random(1,t(1))<=t(0)) return duel(z,t) return 0 ] dpf int n,d,p [ /* display proper fraction n/d as decimal number /* with p digit precision int w w=10*n/d if (p) [ MC w,14 dpf 10*n%d,d,p-1 /* recursive call ] ] /* hit probabilities: zee z(0)/z(1) toby t(0)/t(1) luna l(0)/l(1) int z(1),t(1),l(1) /* pointers to global numerators and denominators contest [ l(0)=l(1)=1 /* luna is a purrfek markscat int zr(1) /* pointer to work numerator and denominator zr(1)=1000 /* rounds int zwins1,zwins2 int i for (i=1;i<=zr(1);++i) [ zwins1=zwins1+duel(z,t) zwins2=zwins2+duel(z,l) ] /* pointers to work numerators and denominators int f(1),n(1),m(1) int o(1);o(0)=o(1)=1 zr(0)=zwins1 fracmult(t,zr,n) fracsub(o,t,f) /* probability t misses=1-t(0)/t(1) zr(0)=zwins2 fracmult(f,zr,m) fracadd(n,m,l) ps "0.";dpf(l(0),l(1),5) ] fracadd int nd1(1),nd2(1),nd(1) [ /* add fractions nd(0)=nd2(1)*nd1(0)+nd1(1)*nd2(0) nd(1)=nd1(1)*nd2(1) ] fracsub int nd1(1),nd2(1),nd(1) [ /* subtract fractions nd(0)=nd2(1)*nd1(0)-nd1(1)*nd2(0) nd(1)=nd1(1)*nd2(1) ] fracmult int nd1(1),nd2(1),nd(1) [ /* multiply fractions nd(0)=nd1(0)*nd2(0) nd(1)=nd1(1)*nd2(1) ] main [ char black,red,green,yellow,blue,magenta,cyan,white char lo,hi black='0';red='1';green='2';yellow='3' blue='4';magenta='5';cyan='6';white='7' lo='0';hi='1' hilo hi color red;pl" ." color white;pl" / \" color yellow;pl".";color white;ps" _ ";color blue;ps"." pl"";pl"" color blue;ps "truel.tc - 8/18/9 - tct";color white;pl"" pl"Please enter hit probabilities for the first two shooters.";pl"";pl"" ps"Player 1 numerator (e.g. 1): ";z(0)=gn ps"Player 1 denominator (e.g. 3): ";z(1)=gn ps"Player 2 numerator (e.g. 2): ";t(0)=gn ps"Player 2 denominator (e.g. 3): ";t(1)=gn int howmany;howmany=11 while (--howmany) [pl"simulation";pn 10-howmany;ps ": ";contest] pl"";pl"The expected value for example probabilities is 25/63=0.3968" pl"" color blue pl"Please visit http://preview.tinyurl.com/mpolsb" pl"" hilo lo ]