%simulate the top of an inning
%assume identical batters
triple = 0
double = 0
single = 0
homerun = 0
outs = 0
runs = 0
base1 = 0 %set to one of player on first base, else 0
base2 = 0
base3 = 0
while outs < 3
%what is the result of the at bat?
x = rand(1);
if x <= .02
triple = triple + 1;
if base1 == 1
runs = runs + 1;
base1=0;%reset first base to be empty
end
if base2 == 1
runs = runs + 1;
base2=0%/reset second base to be empty
end
if base3 == 1
runs = runs +1;
%third base has new runner so don't reset
end
elseif x > .02 & x <=.06
double = double + 1;
if base3 == 1
runs = runs + 1;
base3=0;%reset base to be empty
end
if base2 == 1
runs = runs + 1;
%/second base has new runner
end
if base1 == 1
base3 = 1; %reset third base to be empty
base1 = 0;%send player on first to third
end
elseif x > .06 & x <=.16
homerun = homerun + 1;
runs = runs + 1
if base1 == 1
runs = runs + 1;
base1=0;%reset first base to be empty
end
if base2 == 1
runs = runs + 1;
base2=0%/reset second base to be empty
end
if base3 == 1
runs = runs +1;
base3 = 0; %reset third base to be empty
end
elseif x > .16 & x <=.28
single = single +1;%note this includes walks and hit-by-pitch
if base3 == 1
runs = runs + 1;
base3=0;%reset first base to be empty
end
if base2 == 1
base3=1;
base2=0;%/reset second base to be empty
end
if base1 == 1
base2 = 1; %reset third base to be empty
end
base1 = 1; %send player to first
else
outs = outs + 1;
end
end%with while
single
double
triple
homerun
outs
runs