# Created by Octave 4.4.1, 14-Nov-2018 %LawnBowlingFunctG -- Copyright Giovanni Ciriani %Function to Simulate several ends of Lawn Bowling with 2 players in 2 Teams % distances are gamma distributed, other distributions may be used as well function WinRatio = LawnBowlingFunctG(beta1, beta2, ends) %computes the probability of player 1, with Poisson average L1 % to win over player 2 with Poisson average L2 % or beta parameters for Gamma probability distribution - larger beta is closer to jack %input parameters %printf('Function Lawn Bowling Gamma... '); Teams = 2;%number of teams - code doesn't work for a different number of teams yet Players = 1; %number of players in each team BperP = 4;%bowls per each player nBowls = BperP*Players;%number of bowls played by each team at each end %ends = 200;%number of games played alpha = 7;%shape parameter measured on green %beta1 = 1000;%distance parameter of player 1, mu = alpha/beta %beta2 = 990;%distance parameter of player 2 %L1 = 990%player 1 Poisson average and variance parameters %L2 = 1000%player 2 Poisson average and variance parameter Dist = [beta1*randg(alpha, ends, nBowls), beta2*randg(alpha, ends,nBowls)]; %random distance of bowls of average L %start timing %tic %reorder shots for scoring [sortDistance,bowlIndex] = sort(Dist'); %sD = sortDistance';bI = bowlIndex';%debug auxiliary variables %score each game played for i = 1:ends%score each end A =find(bowlIndex(:,i)nBowls,1);%position of best bowl of team B nextBowl = max(A,B);%Bowl of loser to compare to next one if identical shot while sortDistance(nextBowl,i) == sortDistance(nextBowl-1,i); %if next shot is identical nextBowl = nextBowl-1;%try comparison with further shot of winner if nextBowl == 1, break endif;%but stop going to next if at the bottom end if A>B A = nextBowl;%winner is Team B and A else B = nextBowl;%winner is Team B endif score(i) = B-A;%score for that i-th play end %stop timing %toc score; total = sum(score); totalPoints = sum(abs(score)); bins = [min(score): max(score)+1];%added 1 to prevent Hist error %in case always same score at each end Freq = hist(score,bins);%histogram of scores Perctg = hist(score,bins,1);%histogram normalized to 100% Wins = sum(Freq(find(bins>0)));%number of Wins for team A Draws = sum(Freq(find(bins==0)));%number of Draws for team A Losses = sum(Freq(find(bins<0)));%number of Losses for team A WinRatio = Wins / ends;%calculate the percentage of Wins %figure(1) %hist(score, bins,1) end