Game Development Reference
In-Depth Information
Monte Carlo Simulation
If you have a simple deterministic mechanic that simulates some real-world effect,
then it doesn't take long to see if it works correctly. For example, the gears in a car's
drive train govern the relationship between the speed of a car's engine and the
speed of the car's wheels. It's a fixed mathematical relationship and easy to com-
pute. On the other hand, if you design a complicated mechanic with all sorts of
factors, it may be difficult to predict how it will behave. You don't have time to test
all possible outcomes to make sure they all make sense. Instead, you can do some-
thing called Monte Carlo simulation.
NOTE This method
of simulating a process
with a variety of
random inputs is
named after the famous
casino at Monte Carlo.
Gambling games all
use random values
(shuffled cards, thrown
dice, and so on), but
by repeated simulation
a casino can compute
the probable profit-
ability of a particular
game.
In Monte Carlo simulation, you make a large number of test runs of your system
using random inputs, and record the results in a file. Then you can examine the file
and make sure that the outcomes reflect the behavior that you expect. Here's an
example: Many sports games let the player manage a team throughout a whole sea-
son, and play each match that the real team would play. The game simulates all the
other matches in the league season (the ones not involving the player's team) auto-
matically. If you don't want the machine to play each simulated match through
moment by moment, which the player probably won't want to wait for, you will
need to design a mechanic that fakes it; an algorithm that generates the win-loss
results for all the other matches without really playing them. Your mechanic will
probably be based on the attributes (such as the performance characteristics) of the
athletes on the team. (The section “Simulating Matches Automatically” in Chapter
16, “Sports Games,” discusses this issue in more detail.) But how can you be sure
that your mechanic produces realistic results? You can try it by hand a few times,
but that's not enough to constitute a serious test.
To perform a Monte Carlo simulation, randomly generate t wo teams of athletes, w ith
a variety of random attribute settings for each athlete, then apply your mechanic to
them and record which team wins. Do this repeatedly, 1000 times or so. Afterwards,
analyze the data from the simulations to see if any anomalies occurred. Did a weak
team ever beat a strong team? Did it happen often, or was it a fluke? If it was a
fluke, happening once in 1000 times, that's OK—if sports matches were completely
predictable they'd be boring. But if it happened often, you know your mechanic
has a problem. If you know statistical methods, you can compute the correlation
between the inputs (relative team strength) and the outputs (who won) and make
sure that there's a positive correlation between strength and victory.
People use Monte Carlo simulation for all sorts of things: to predict profits when
people buy products at different price points, to predict the failure rate of new
products, and so on. Microsoft Excel and other spreadsheet programs contain built-
in tools for performing Monte Carlo simulations. If you can define your mechanic
in a spreadsheet, you can easily use these tools.
Search WWH ::




Custom Search