Okay, here’s my blog post about making an MLB baseball simulator:

So, I’ve always been a huge baseball fan, and I also love messing around with code. I decided to try and put the two together, making my own little baseball simulator. I figured it’d be a fun weekend project, and I wanted to see if I could actually make something that, you know, worked.
I started out pretty basic. No fancy graphics or anything, just pure numbers. I used Python because, well, that’s what I’m most comfortable with, and it handles data pretty well. First, I needed to get some player stats. Instead of diving into massive databases, I just grabbed some basic stats for a couple of my favorite teams from the past season. Just simple stuff: batting average, home runs, RBIs, ERA for pitchers, that kind of thing.
The next thing I did was create some simple functions. One for simulating an at-bat. This was basically a random number generator, tweaked to reflect the batter’s average. So, a guy with a .300 average would have a 30% chance of getting a “hit” in my super-simple model. Another function simulated an inning, just calling the at-bat function nine times (or more, if there were extra innings).
- At-Bat Function: Random number generator based on batting average.
- Inning Function: Calls the at-bat function repeatedly.
- Game Function: Calls the inning function for both teams, keeping score.
I then put it all together in a game function. This basically ran the inning function for the home team, then for the away team, and kept track of the score. I added some extra bits to handle things like home runs (which were just another random chance, influenced by the player’s home run stats) and RBIs (if a guy got a hit, and there were runners on base, they had a chance to score).
My first few “games” were hilarious. Scores were like 20-15, totally unrealistic. It was clear my probabilities were way off. I spent a good chunk of time just tweaking those numbers, trying to get something that looked at least vaguely like a real baseball game. I realized that even with simple parameters, It’s hard to simulate.

After a lot of fiddling, I finally got something that produced, shall we say, plausible results. Games were usually in the single digits, home runs weren’t happening every other at-bat, and the better team (based on the stats I used) usually won. It was far from perfect, but hey, it was working!
What I Learned
This little project taught me a few things. First, even a simple simulation is more complicated than you think. Second, I had to tweak many parameters to make things work, and balancing all those little variables is a real challenge. And finally, it’s super satisfying to build something from scratch and see it actually do something, even if it’s just spitting out fake baseball scores.
I’m thinking of adding more features later – maybe stolen bases, different types of hits, pitching changes… who knows! For now, I’m happy with my little number-crunching baseball machine.