Sub topic 11
Monte Carlo Simulation with RAND
This chapter assumes you have read the earlier chapters on workbook structure (Params, calculations, outputs), probability and expected value, and derived metrics such as expected damage and time to kill. You should be comfortable with fill-down, absolute references like $B$2, and functions such as AVERAGE, PERCENTILE and COUNTIF.
Why simulate when you have formulas
Closed-form formulas give averages. For a drop with chance p, the expected number of attempts is 1/p. Players do not experience averages. They experience one run, and that run can be much shorter or much longer than the mean.
A Monte Carlo simulation repeats a random process many times and looks at the spread of results. In a spreadsheet, each row is one trial. Each trial uses RAND(), which returns a random decimal from 0 up to (but not including) 1. You then summarize the rows to answer questions like these:
- How many attempts does the unluckiest 1 in 10 players need?
- How often does a fight end within 10 swings?
- How often does a player see no drop after the expected number of attempts?
Use simulation when the question is about variance, streaks, or a process that is awkward to write as one formula. When a closed form exists, keep it next to the simulation and use it as a check.
How RAND behaves
RAND() takes no arguments. Each call returns an independent value, so two RAND() calls in one formula give two different numbers. This is what you want when one formula needs a hit roll and a separate crit roll.
RAND is volatile. The spreadsheet recalculates it whenever anything on the sheet changes, so every edit reshuffles all your trials. This has three consequences:
- Your results change each time you look, which is correct but can confuse you when you compare runs.
- There is no seed. You cannot reproduce an exact run.
- A large trial table slows the workbook, because every cell recalculates on every edit.
The simplest trial: one event
A yes/no event with probability p is RAND() < p. The comparison returns TRUE or FALSE. Wrap it in IF to get a number:
=IF(RAND() < Params!$B$7, 1, 0)This works because RAND() is uniform. The share of values below p is exactly p. Fill this down 1,000 rows and AVERAGE of the column should land near p, with some noise.
Attempts until a drop
Suppose a boss drops an item with chance B2 per kill, and you want the number of kills needed to get it. The straightforward way is to roll once per attempt until one succeeds, but that needs a variable number of cells per trial. A formula can instead convert one random number into the attempt count directly:
=MAX(1, ROUNDUP(LN(1 - RAND()) / LN(1 - $B$2), 0))Here is how it works:
- The chance of no drop in
nattempts is(1 - p)^n. - A random number
ufromRAND()stands for a point on that curve. Setting(1 - p)^n = 1 - uand solving fornwith logarithms givesn = LN(1 - u) / LN(1 - p). ROUNDUPturns the fractional result into a whole attempt count.MAX(1, ...)covers the rare case whereRAND()returns exactly 0, which would otherwise give 0 attempts.
This technique is called inverse transform sampling. It produces the same distribution as rolling attempt by attempt, using one RAND() per trial.
Edge cases:
- If
B2is 0, the formula divides by zero. A drop that never happens has no attempt count. - If
B2is 1,LN(0)is an error. Handle it with=IF($B$2>=1, 1, ...)if your parameters can reach 100%.
Setting up the trial table
Put p in Params. On a Sim sheet, put trial numbers 1 to 1,000 in column A and the formula above in column B. Then add a summary block:
| Statistic | Formula | Meaning |
|---|---|---|
| Mean attempts | `=AVERAGE(B5:B1004)` | Should be near `1/p` |
| Median attempts | `=MEDIAN(B5:B1004)` | The typical player |
| Unlucky (90th percentile) | `=PERCENTILE(B5:B1004, 0.9)` | 1 in 10 players needs more than this |
| Very unlucky (99th) | `=PERCENTILE(B5:B1004, 0.99)` | Tail experience |
| Worst trial | `=MAX(B5:B1004)` | Noisy, do not tune to it |
| No drop by expected attempts | `=COUNTIF(B5:B1004, ">"&ROUND(1/Params!$B$6,0))/COUNT(B5:B1004)` | Share who exceed the average |
For p = 0.02 (a 2% drop), the closed-form values are:
| Measure | Exact value |
|---|---|
| Mean attempts | 50 |
| Median attempts | about 35 |
| 90th percentile | about 114 |
| 99th percentile | about 228 |
| Chance of no drop in 50 attempts | `0.98^50`, about 36% |
The last row surprises many designers. A player who has done the "expected" 50 attempts has a 36% chance of still having nothing. If your simulation shows that rate and your game has no pity system, expect frustrated players.
You can check one of these against the spreadsheet formulas from the probability chapter. The chance of at least one drop in C2 attempts is:
=1 - (1 - B2) ^ C2For B2 = 0.02 and C2 = 50 this gives about 0.64. In the simulation, =COUNTIF(B5:B1004, "<=50")/COUNT(B5:B1004) should land near 0.64, give or take noise.
Simulating a short fight
Now a case where a closed form is less convenient. A player makes 10 swings at a target. Each swing hits with chance Params!B7, crits with chance Params!B8 if it hits, and a crit multiplies damage by Params!B9. Base damage is Params!B6. You want the chance the fight kills a target with health Targets!B2 within 10 swings.
Lay out one row per fight, with swings in columns B to K. Each swing cell is:
=Params!$B$6 * (RAND() < Params!$B$7) * (1 + (RAND() < Params!$B$8) * (Params!$B$9 - 1))Comparisons like RAND() < x evaluate to TRUE or FALSE, and multiplying them by a number treats them as 1 or 0. This works in Excel and Google Sheets. If a miss happens, the first factor is 0 and the whole swing is 0. The two RAND() calls are independent, so the hit roll and crit roll do not interfere.
Then add two columns:
=SUM(B5:K5)for total damage in column L, and
=IF(L5 >= Targets!$B$2, 1, 0)for a kill flag in column M. The kill probability is =AVERAGE(M5:M504) over 500 fights.
Compare this with the expected damage formula from the derived-metrics chapter:
=B2 * C2 * (1 + D2 * (E2 - 1))Ten swings multiplied by this value gives the average total damage. The average of column L should match it. The simulation adds what the average hides: the share of fights that fall short of the target's health, and how far short.
How many trials are enough
A simulated estimate has sampling error. For a yes/no result with true probability p over n trials, the standard error is SQRT(p * (1 - p) / n).
- With
p = 0.5and 500 trials, the error is about 0.022, so a reading of 52% could be anywhere from about 48% to 56%. - With 5,000 trials, the error drops to about 0.007.
- Error shrinks with the square root of
n, so cutting it in half takes four times as many trials.
For the mean attempts of a 2% drop, the standard deviation of one trial is about 49.5, so 500 trials give a standard error of about 2.2 attempts. A mean reading of 48 or 52 is normal.
Practical rules:
- Rerun the sheet several times (press F9 or edit a blank cell). If a statistic swings by more than you can tolerate, add rows.
- Rare events need many more trials. An event with 0.5% probability appears only about 2 times in 500 trials, so its estimate is mostly noise.
- Percentiles in the far tail, such as the 99th, are noisier than the mean. Use more trials when you quote them.
- Stop at the precision your decision needs. Choosing between a 2% and 3% drop rate does not need three decimal places.
Common errors
- Reading one run as the answer. Always look at several recalculations or enough trials to stabilize.
- Using one
RAND()for two decisions. Store it in a helper cell only if you want the two decisions correlated, which is rare. - Comparing with
<=vs<carelessly. For a continuousRAND(), the difference is negligible, but a consistent<keeps edge cases predictable. - Mixing live and frozen cells. Half-frozen tables produce confusing results. Freeze the whole range.
- Tuning to the simulation noise. If changing a parameter moves a statistic by less than the standard error, you have not found an effect.
Exercise
A drop has a 5% chance per attempt. Using the closed form, what is the probability of at least one drop in 20 attempts, and which cell formula would you check it against in a 1,000-row simulation of attempts-until-drop?
Show answer
The closed form is =1 - (1 - 0.05)^20, which is about 0.64. In the simulation, put the attempts-until-drop formula =MAX(1, ROUNDUP(LN(1 - RAND()) / LN(1 - 0.05), 0)) in column B and check =COUNTIF(B5:B1004, "<=20")/COUNT(B5:B1004). It should be near 0.64, give or take about 0.015 of sampling noise.
Exercise
Your simulation of 500 fights shows a kill probability of 0.70. Roughly how wide is the uncertainty, and how many trials would you need to halve it?
Show answer
The standard error is SQRT(0.7 * 0.3 / 500), about 0.02, so the true value is plausibly between 0.66 and 0.74. Halving the error needs four times the trials, so about 2,000.