How can I predict the number of runs scored?
1 view (last 30 days)
Show older comments
I am trying to run a simulation to see how many runs would be scored in a 9 inning baseball game if every batter got a single half the time, and got out the other half of the time. Obviously, after three outs in an inning, the bases would be emptied
2 Comments
James Tursa
on 24 Apr 2019
Edited: James Tursa
on 24 Apr 2019
What have you done so far? What specific problems are you having with your code? Do you know how to simulate an at-bat? Do you know how to simulate runners advancing? Do runners only move one base on a single? If the batter gets out, is it always a strikeout or do runners advance? Etc.
Answers (1)
James Tursa
on 24 Apr 2019
Edited: James Tursa
on 24 Apr 2019
I will get you started. You need to write a code outline for how you would do it, e.g., with commenting, and then fill in the code. For example, a code outline could look something like this:
runners = zeros(1,3); % initialize runners on base for three bases, 0 = not on base, 1 = on base
outs = 0; % initialize number of outs
inning = 1; % initialize inning
phit = 0.5; % initialize probability of a hit
runs = 0; % initialize the number of runs
while( inning <= 9 )
% insert code here to see if you got a hit
% if you got a hit, then
% insert code here to advance all runners and possibly score a run
% else
% insert code here to increase the number of outs for this inning
% if the number of outs is 3, insert code here to reset the states and increment the inning
end
So, I wrote a bunch of comments to myself stating what the code would do. Once you get that to an algorithm you like that you think will work, you would need to replace the comments with actual code and then start testing it.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!