Vault #8: Dungeons & Dragons
In this DISCOVERY Data Science Project, you will do real data science in less than an hour and you will earn this project's card to your collection when you fully complete this project! 🎉
Introduction
Dungeons & Dragons is a tabletop role-playing game in which you and your friends play a group of adventurers and seek to slay monsters, meet NPCs, and become more powerful in a fantasy world. One of the most recognizable aspects is the dice used to determine the outcome of events. The dice are designated by "d" followed by the number of sides. For example, a d6 is a standard 6-sided die. There's also a d4, d8, d10, d12, d20, and d% (the same shape as a d10 but with 00, 10, 20, etc). Fun fact: most of the dice are platonic solids!
In this Project, you will create simulations of dice rolls and explore the statistics of the rolls. Let's nerd out!
Puzzle 1: Creating the Dice
Puzzle 1.1: rollDie Function
If we're going to roll dice, we'll need to create the dice first! We could create a function for every die, but that seems redundant. Let's instead create a more general function rollDie that takes a parameter sides representing how many sides the die has. The die is fair, so each side is equally likely to be rolled!
You may be wondering what we can do about the d%. Luckily, the d% is almost always rolled along with the d10. If you add them, the d% provides the tens digit, and the d10 provides the ones digit. This is essentially the same as rolling a 100-sided die, so we can just use sides = 100!
⚙️ Test Case: Puzzle 1.1: rollDie function
Puzzle 1.2: Rolling Dice
Awesome, we can now roll a die with any number of sides! Let's test it out. Set num_sides equal to your favorite number out of 4, 6, 8, 10, 12, and 20, then run a simulation rolling a die with num_sides sides 10,000 times! Store the results in df under the column "roll".
⚙️ Test Case: Puzzle 1.2: Testing the die
Puzzle 1.3: Visualizing the Result
It may feel natural to create a histogram since the result of dice rolls feels like continuous data being in the range [1, 2, 3, 4, ...]. However, each roll is a single discrete result in one of a finite set of categories.
Since the dice rolls are finite set of categories, the best simple data visualization to use is a bar chart (not a histogram). To create a bar chart, we need to count the number of times each value appears in our DataFrame.
Read the DISCOVERY guide "Creating a Frequency Bar Chart from a DataFrame" to find out more about frequency bar charts:
Create a visualization of your original df as an ordered bar chart of the outcomes of your dice rolls. Make certain that your x-axis is sorted by the category/index value (not the frequency).
Create a Function to Visualize the Result
Make what you just did into a function called visualizeDiceRolls().
- This function must accept one parameter, a DataFrame with the data column
roll. - This function must return a bar plot using
.plot.bar(). - The bar chart must be a frequency bar chart with the x-axis sorted by the category name (not by frequency).
⚙️ Test Case: Puzzle 1.3: visualizeDiceRolls Function
Puzzle 1.4: Rolling a d20
The d20 is used for many things, including determining if an attack hits or misses. The higher the number rolled, the more likely you are to hit. If you roll a 20, you get a critical hit and deal even more damage! Create another simulation where you roll 10,000 d20s (unless num_sides is already 20) and store the results in a DataFrame called df_d20 under the column "roll", and use pandas to calculate the following:
- The mean of the rolls
- The standard deviation of the rolls
- The probability of rolling a 20
Finally, use your visualizeDiceRolls function to visualize this set of rolls:
⚙️ Test Case: Puzzle 1.4: Rolling a d20
Puzzle 2: Rolling With Advantage
Puzzle 2.1: Advantage
Finding the value of a single die rolled once is simple enough. However, in DnD, you can roll with advantage, meaning that you roll two dice and use the higher value. For example, if you roll two d20s and get a 7 and a 15, your value is a 15. Simulate rolling 10,000 d20s with advantage, and store the results in df_d20_adv under the column "roll" (the value you store under this column should be the higher of the two rolls).
Like before, calculate:
- The mean of the rolls
- The standard deviation of the rolls
- The probability of rolling a 20
Notice the differences between these values and the ones for d20 without advantage. The probability of a critical hit is about twice as high!
Use your visualizeDiceRolls function to visualize this set of rolls (df_d20_adv):
⚙️ Test Case: Puzzle 2.1: Advantage
Puzzle 2.2: Disadvantage
Just as you can take the higher of two d20s, you can also take the lower of two d20s, which is called rolling with disadvantage. Let's try implementing it. Do you think it'll be similar to rolling with advantage?
Simulate rolling 10,000 d20s with disadvantage, and store the results in df_d20_disadv under the column "roll". Then calculate:
- The mean of the rolls
- The standard deviation of the rolls
- The probability of rolling a 20
- The probability of a rolling a 1
Use your visualizeDiceRolls function to visualize this set of rolls (df_d20_disadv):
⚙️ Test Case: Puzzle 2.2: Disadvantage
Puzzle 2.3: Triple Advantage
In special cases, when you roll with advantage, you can reroll one of the dice. This is effectively the same as rolling three dice and taking the highest, called "triple advantage"! Simulate rolling 100,000 d20s with triple advantage, and store the results in df_d20_trip_adv under the column "roll".
Now calculate:
- The mean of the rolls
- The standard deviation of the rolls
- The probability of a 20
Use your visualizeDiceRolls function to visualize this set of rolls (df_d20_trip_adv):
⚙️ Test Case: Puzzle 2.3: Triple Advantage
Finally, run the code cell below (don't change anything!) to compare the values for everything we've calculated so far. Notice any patterns?
What's the average of the means with advantage and with disadvantage? Do you recognize this number?
This section was inspired by a video by Matt Parker (Stand-up Maths): https://www.youtube.com/watch?v=X_DdGRjtwAo
Puzzle 3: Damage Rolls
We've rolled a lot of d20s! Let's assume that your attack hit. Now we need to figure out how much damage you deal.
There are a ton of ways to deal damage in DnD, but for this project, you'll be playing a paladin wielding a greatsword (2d6 damage). The 2d6 means that you roll two separate d6s and add them together.
You also have a strength modifier of +3, which you'll add to the damage after the rolls.
Puzzle 3.1: Normal Damage Roll
Simulate a normal damage roll for your greatsword wielding paladin by rolling for damage 100,000 times. Remember that:
- You will roll two separate d6s (don't just double the result!)
- After rolling, add 3 more damage.
Store the results in df_damage under the "damage" column. Remember you can use your rollDie function!
⚙️ Test Case: Puzzle 3.1: Normal Damage Roll
Puzzle 3.2: Damage Visualization
In Puzzle 1.3, you created a visualizeDiceRolls to visualize the result of dice rolls. Create a very similar function below to create a visualization of the damage done, using the same technique as Puzzle 1.3.
The only key differences will be (1): the name of the function (visualizeDamage) and, (2): using the data in the DataFrame column "damage". Create the visualizeDamage function.
You may want to refer back to Puzzle 1.3 to help out here.
Let's visualize the damage outcomes of your paladin:
Puzzle 3.3: Normal Damage Statistics
We'll now calculate two basic statistics:
- The mean damage
- The standard deviation of the damage
⚙️ Test Case: Puzzle 3.3: Normal Damage Statistics
Puzzle 3.4: Divine Smite
In the previous part, you saw your paladin is dealing an average (and median) of 10 damage, but we can do better than that! :)
Paladins are able to add divine smite, dealing an EXTRA 3d8 damage if a level 2 spell slot is used. (That's three rolls of d8s, in addition to the original two d6s!)
Simulate rolling for damage with divine smite 100,000 times, and store the results in df_damage_smite under the "damage" column. Remember to still add your strength modifier (+3) afterwards.
Now, visualize this damage:
Finally, find a few basic statistics:
⚙️ Test Case: Puzzle 3.4: Divine Smite
Puzzle 3.5: Critical Hit with Divine Smite
Let's say you got a 20 on your attack roll -- it's a critical hit!
A critical hit means you will roll twice as many dice for damage! Simulate rolling for damage 100,000 times with both divine smite and a critical hit. That is, roll 4d6, 6d8, and then add your strength modifier (+3).
That's a lot of dice! Store the results in df_damage_smite_crit under the "damage" column.
Now, visualize this damage:
And find the mean and standard deviation:
⚙️ Test Case: Puzzle 3.5: Critical Hit with Divine Smite
Damage Summary
The cell below provides a summary of all the calculations from this section (no modifications needed, you can just run it):
Notice any patterns? If you subtract 3 (the strength modifier) from the means, what's the relationship between the mean with smite and the mean with smite and crit?
Validate and Complete This Project!
Congratulations on finishing this DISCOVERY Data Science Project! 🎉🎉
To validate your entire project, your entire code will run from top-to-bottom on this page and each test case will be validated one final time. If everything looks good, you'll earn the card for completing this project: