Quote:
Originally Posted by Your Mom
I have 9 probabilities of MLB players making outs. I want to run 100k simulations to see how often on average each player will bat until 27 outs are made. How?
I think this could be read a couple different ways but what I assumed you wanted was something like this:
Player 1 out 90% of the time, player 2: 80%, player 3: 70%...
Simulate Player 1 at bat and see if he's out
Simulate Player 2 at bat and see if he's out
Simulate Player 3 at bat and see if he's out
...
repeat until 27 outs, then report how many batters it took to get there.
If that's the case, list your 9 players probabilities in cells A1 through A9 in the format 0.xxx (any number 0 through 1.000). Right click on the bottom where it says "Sheet1" and click "View Code". Copy and paste this into the big empty text box that's taking up the majority of the screen:
Code:
Sub Simulate()
Dim Outs, Batters As Integer
For i = 1 To 100000
Randomize
Outs = 0
Batters = 0
Do
If Rnd < Cells((Batters Mod 9) + 1, 1).Value Then
Outs = Outs + 1
End If
Batters = Batters + 1
Loop Until Outs = 27
Cells(i, 2).Value = Batters
Next i
End Sub
Go back to your worksheet. Press Alt+F8. Run "Simulate". Wait a couple minutes. The results of your simulation will be listed in column B. If you want the average, you can type "=AVERAGE(B1:B100000)" into any empty cell.
NOTES:
If you're using Excel 2003 or earlier, or the compatibility mode in Excel 2007+, there is no row 100,000 and this code will give you an error once it reaches the last row. In that case I would change where the code says "100000" to "50000", run it once, then change where the code says "Cells(i, 2).Value = Batters" to "Cells(i, 3).Value = Batters", then run it again. This will put the results of your simulations into rows B and C, 50,000 results each. Change your average formula to "B1:C50000".
Even if you have Excel 2007+, it wouldn't be a bad idea to change the code like I mentioned above, only cut it down further, maybe 10,000 simulations in column 2. Then run it again using column 3, 4, etc until you have 10 columns of 10,000 simulations each. If you jump straight in to 100,000 your computer will lock up for a while until it's finished all of them. FWIW my old computer takes 5 seconds to run through 10,000 iterations of this code. I'm not going to try it at 100,000, but it would take longer than 5 sec*10, and even if it was only a minute, a single minute feels like a really long time when you're waiting to see if your code did what you want it to do.
Final note, I took your first sentence to mean exactly as you said it, the probability of each batter making an out. I'd expect your values to be on the upper side of .500, with higher values meaning worse batters. I don't know enough about baseball to know if that's a common stat, all I know is that if you're using batting averages or some other number where higher is better, this will be calculating backwards.