humancode.us

In Praise of Brute Force

January 19, 2020

My middle-schooler daughter loves Dungeons and Dragons. She, along with other players, co-DM1 an online version of the game.

She came to me with a question regarding a new game dynamic that she was considering. Here are its parameters:

  • There shall be 30 days in a month
  • There shall be two 12-hour periods per day
  • As each period arrives, there shall be a 50% chance of bad weather
  • During a period of bad weather, there shall be a 20% chance that an enemy NPC appears to attack your group

The question: Given the above rules, how many enemy NPCs can players expect to encounter in a month of play, and how would changing the various probabilities affect the outcome?

Although this is not a very difficult problem, I realized that in order to explain why multipyling numbers worked, I needed to explain the concepts of Expected Values and conditional probabilities. But I soon realized there was another way.

I opened an editor and wrote a python program. Within 10 minutes, we had a program that would simulate 10,000 months and report the simulated answer. We read the code together and she easily followed how the program worked. We played around with the constants until she was happy with the outcome.

When faced with a logical or mathematical problem, we can often be tempted to reflexively come up with an analytic solution. While such solutions are satisfying and completely capture the problem space, they can sometimes be difficult to explain or derive.

Before hunkering down to find an answer, ask yourself: are you really interested in the closed-form solution, or just the answer? If you only want the latter, a brute-forced solution computable in a reasonable time is just as good. Plus, you’ll save yourself from reliving Statistics 101 along the way.

  1. Dungeon Master