Simulating Dice throws – the correct way to do it in excel

Posted on August 13th, 2008 in Learn Excel , simulation - 11 comments

dice-throws-excel-simulating - correctlyIf you ever had to simulate random outcomes in excel sheet, you might have already heard of about the spreadsheet function rand(), this little function generates a random fraction between 0 and 1 whenever you use it. So I usually write =round(rand()*12,0)int(rand()*12)+1 if I need a random number between 0 to 12. Of course, if you have analysis tool pack installed like I do, then we can use randbetween(0,12) to do the same.

In order to simulate a dice throw, thus you can use round(rand()*5,0)int(rand()*6)+1.

So, what would you do if you need to simulate the face total when you throw 2 dice?

round(rand()*10,0)int(rand()*11)+2 ?

Wrong

Why? Apparently a random number between 2 and 12 (1 is not possible as the minimum you can get when you throw two dice is 2) doesn’t simulate 2 dice throws properly.

The CORRECT way to do this is instead generate 2 individual random numbers and add them up, like:

round(rand()*5,0)int(rand()*6) + 1 + round(rand()*5,0)int(rand()*6) + 1

Here is why this is correct way to simulate dice throws using random number generator functions:

I have ran these 2 random functions each for 2500 times and plotted the distribution:

excel-simulate-dice-throw-distribution

As you can see, the left plot of int(rand()*11)+2 tells that each of the 11 possibilities (2,3,4,5,6,7,8,9,10,11) are equally likely. But that is not what happens when you throw a dice, you see an awful lot more 5,6,7,8 than you see a perfect 12 or 2. And there is a reason for that, the distribution of 2 dice throws is actually a bell curve, and when you use int(rand()*6) + int(rand()*6) + 2 the distribution is bell curvish.

Update: Thanks to Jon for pointing out that round() is not the choice if you want random integers, you should use int instead. See his explanation in the comments and the illustration here.

I have used this logic to simulate monopoly board game and prove that it is not really that random.

More on games: Bingo / Housie ticket generator excel sheet

| More
Subscribe for PHD Email updates and get a free excel e-book with 95 tips & tricks

Comments
alpha bravo August 14, 2008

You have an interesting point, but the bell curve theory is nonsense. Certainly it is not what you would want, even if it were true.

Karl August 14, 2008

Alpha Bravo – Although not a distribution curve in the strict sense, is does reflect the actual results of throwing two physical dice.

And reflects the following . .
There is 1 way of throwing a total of 2
There are 2 ways of throwing a total of 3
There are 3 ways of throwing a total of 4
There are 4 ways of throwing a total of 5
There are 5 ways of throwing a total of 6
There are 6 ways of throwing a total of 7
There are 5 ways of throwing a total of 8
There are 4 ways of throwing a total of 9
There are 3 ways of throwing a total of 10
There are 2 ways of throwing a total of 11
There is 1 way of throwing a total of 12

Chandoo August 14, 2008

@alpha bravo … welcome… :)

either your comment or your dice is loaded ;)

I am afraid the distribution shown in the right graph is what you get when you throw a pair of dice in real world. As Karl already explained, it is not random behavior you see when you try to combine 2 random events (individual dice throws), but more of order due to how things work.

@Karl, thanks :)

Jon Peltier August 14, 2008

When simulating a coin toss, the ROUND function you used is appropriate. However, your die simulation formula should use INT instead of ROUND:

=INT(RAND()*6)+1

Otherwise, the rounding causes half of each number’s predictions to be applied to the next higher number. Also, you’d get a count for 7, which isn’t possible in a die.

To illustrate, I set up 1200 trials of each formula in a worksheet and counted the results. The image here shows the table and a histogram of results:

http://peltiertech.com/WordPress/wp-content/img200808/RandonDieTrials.png

Chandoo August 14, 2008

@Jon: thanks for pointing this out. You are absolutely right. INT() is what I should I have used instead of ROUND() as it reduces the possibility of having either 1 or 6 by almost half that of having other numbers.

this is such a good thing to learn, helps me a lot in my future simulations.

Btw, the actual graphs I have shown were plotted based on randbetween() and not from rand()*6, so they still hold good.

Updating the post to include your comments as it helps everyone to know this.

Jon Peltier August 14, 2008

By the way, the distribution is not a Gaussian distribution, as Karl points out. However, when you add the simulations of many dice together (i.e., ten throws), the overall results will approximate a Gaussian distribution. If my feeble memory serves me, this is the Central Limit Theorem.

Chandoo August 14, 2008

@Jon, that is right, you have to nearly throw infinite number of dice and add their face counts to get a perfect bell curve or Gaussian distribution, but as the central limit theorem suggests, our curve should roughly look like a bell curve… :)

YourFifthGradeMathsTeacher January 25, 2010

I’m afraid to say that this is a badly stated and ambiguous post, which is likely to cause errors and misunderstanding.
Aside from the initial use of round() instead of int(),.. (you’ve since corrected), you made several crucial mistakes by not accurately and unambiguously stating the details.

Firstly, you said:
“this little function generates a random fraction between 0 and 1″
Correctly stated this should be:
“this little function generates a random fraction F where 0 <= F < 1".

Secondly, I guess because you were a little fuzzy about the exact range of values returned by rand(), you have then been just as ambiguous in stating:
"I usually write int(rand()*12)+1 if I need a random number between 0 to 12".
(that implies 13 integers, not 12)

Your formula, does not return 13 integers between 0 to 12.
It returns 12 integers between 1 and 12 (inclusive).
– As rand() returns a random fraction F where 0 <= F < 1, you can obviously can only get integers between 1 and 12 (inclusive) from your formula as stated above, but clearly not zero.

If you had said either:
"I usually write int(rand()*12) if I need a random number between 0 to 11 (inclusive)",
or:
"I usually write int(rand()*12)+1 if I need a random number between 1 to 12 (inclusive)"
then you would have been correct.

Unfortunately, you FAIL! — repeat 5th grade please!

Your Fifth Grade Maths Teacher

RSS feed for comments on this post. TrackBack URI

Leave a comment

   Name (required)

   E-mail (required, never displayed)

   URL


If you have a question, please ask in the forums

Recommended Excel, Charting, VBA books