Jump to content

Article: Dice and Random Numbers


Recommended Posts

Dice, Random Numbers, and Statistics for BoA Designers

By: Stareye


Blades of Avernum offers a powerful get_ran( num_dice, min, max ) command. However, fully understanding it requires a bit of statistics. Not grasping these results can produce outcomes in the game a designer did not fully expect. I'm going to go through how the results are distributed and discuss the implications on design.


The simplest case is when we set num_dice to one. This uniformly samples a number between min and max. So the command get_ran(1,1,100) will produce a number between 1 and 100 where each number has an equal chance of occurring. This is useful if you want to create an event with multiple random outcomes. To do this, assign the result to a variable (call it r) and go through a series of if-then checks on the value of r. A sample code is as follows:


r = get_ran(1,1,100);if ( r <= bin1 )	{event1}else if ( r <= bin2)	{event2}else	{event3}


Note that result of get_ran was assigned to a variable because each time you call get_ran, it generates a new random number -- if you need to check a random number multiple times call get_ran once, store the result, and check the variable. The probability of each event occurring is the relative width of the bin given by values bin1, bin2, and bin3. A possible application for this is have random text bubbles occur on a specific character or a creature script deciding which programmed ability to use. In this case the probability distribution is easy to understand.


Now what happens if we raise the number of dice in the get_ran command? The results are now no longer uniform. The command get_ran(10,1,4) produces a number from 10-40. However, it is not distributed evenly unlike the previous ones with one die -- note that the call get_ran(1,10,40) would produce an evenly distributed set of numbers.


The most likely result of our get_ran(10,1,4) command is 25 and numbers close to that. Numbers near 10 or 40 are incredibly rare. This is because when you add together lots of rolls on the die, there are far more ways to get 25 than there are 10. In fact, to get 10 requires all ten rolls equal 1. The probability of this is (1/4)^10 or around one in a million compared to 1/31 (around 3%) for the get_ran(1,10,40) call.


In other words, the results from get_ran(1,10,40) and get_ran(10,1,4) are incredibly different even though they have the same range of values. Designers should keep this in mind because our previous code would produce very different results statistically if I used one versus the other. Good applications for large numbers of die are things like dealing damage or other things where you want randomness, but not a huge amount of variation.


So how is the result of get_ran(10,1,4) actually distributed? This is not an easy question to answer because it requires a bit of calculus to solve correctly. However, we can often get a pretty good guess by something called the Central Limit Theorem. This handy theorem states that if we add together a lot of independent, identically distributed random variables (as we are with adding 10 dice with values from 1 to 4) the result will roughly be a normal distribution in the vicinity of the mean average. This probability distribution if very peaked about the mean average (25 in this case) and drops off very quickly as we go further away from it. In other words, high chance of getting near 25 and very low getting around 10 or 40. Know that the more dice we use, the more it follows this behavior.


Note that damage from weapons (not considering armor) works this way. If a weapon produces 10-40 damage. You can just think of that number as around 25 before any damage reduction from armor kicks in. This is useful when deciding on how much armor and defensive stuff to give things so you have a good idea of how much damage is actually being done in combat.


The key point with all of this is that the number of dice dramatically affects how the random numbers come out of the get_ran command. The results of, say, get_ran(20,1,3) are very different statistically than get_ran(1,20,60). Not understanding this can lead to errors in assuming how likely specific random events are in your scenario. The get_ran command is very powerful, but make sure you script in mind with how the numbers it produces are distributed.

Link to comment
Share on other sites

Most importantly, if you need to use the same random number more than once (for example, if you want to generate a random number from 1 to 100, do one thing if it's less than 25, another thing if it's between 25 and 80, and yet another thing if it's more than 80), make sure to assign it to a variable and check the value of the variable! If you use the get_ran call three times, you'll get a different number each time. This is a common newbie mistake when working with random numbers.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...