Rolling an N-sided Dice N times

A version of this question came up in class: if you roll an n-sided dice n times, what is the probability that you'll roll a particular side?

You know that the chance of rolling a particular side on the first roll is $\dfrac{1}{n}$. So, you might think that rolling a particular side out of two rolls would be $\dfrac{1}{n} \times \dfrac{1}{n}$. But that is the probability of rolling the same side twice in a row.

Perhaps it is easier to think about the probability of not rolling a side. That would be $\dfrac{5}{6}$, or, in general, $\dfrac{n - 1}{n}$. So the probably of not rolling a particular side twice is:

$\dfrac{n - 1}{n} \times \dfrac{n - 1}{n}$

or for rolling the dice $n$ times:

$\left(\dfrac{n - 1}{n}\right) ^ n$

If we then subtract that from 1 to get the probability of rolling a particular side:

$1 - \left( \dfrac{n - 1}{n} \right) ^ n$

Let's do some simulations to see if this matches what we would expect:

In [1]:
import random

Let's roll a 6-sided dice 6 times:

In [2]:
rolls = [random.randint(1,6) for i in range(6)]
print(rolls)
[5, 6, 2, 1, 4, 4]

And count the number of times it rolled each side:

In [3]:
[rolls.count(i) for i in range(1,7)]
Out[3]:
[1, 1, 0, 2, 1, 1]

And make a function to test, for arbitrary number of sides:

In [4]:
def test(n):
    rolls = [random.randint(1,n) for i in range(n)]
    print([rolls.count(i) for i in range(1,n + 1)])
In [5]:
test(6)
[1, 2, 0, 0, 0, 3]
In [6]:
test(100)
[0, 1, 3, 4, 0, 0, 2, 0, 2, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 2, 1, 1, 0, 0, 3, 3, 1, 1, 1, 0, 0, 0, 2, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 2, 1, 2, 1, 4, 1, 1, 0, 4, 1, 0, 4, 0, 2, 2, 1, 0, 2, 3, 1, 1, 0, 0, 1, 1, 1, 6, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 3, 0, 0, 3, 1, 0, 1, 0, 3, 0, 1]

Now, let's see how many times it rolled at least one of each number, total those up, and divded by the sides:

In [7]:
def test(n):
    rolls = [random.randint(1,n) for i in range(n)]
    return sum([1 for count in [rolls.count(i) for i in range(1,n + 1)] if count > 0])/n
In [13]:
test(6)
Out[13]:
0.666666666666667
In [16]:
test(100)
Out[16]:
0.63
In [17]:
test(10000)
Out[17]:
0.6305

Does that match what we would expect?

In [18]:
1 - ((10000 - 1)/10000) ** 10000
Out[18]:
0.63213895356703

Yes, it does! Cool!

In fact, as you make $n$ larger (both experimentally and through the equation) you will see that it gets closer and closer to a particular number.

This is a well-known equivalency:

$\lim_{n \to \infty} ((n - 1)/n) ^ n = 1/e$

So, the probability of rolling a particular side of an n-sided dice after n rolls is:

$1 - 1/e$

In [21]:
import math
print(1 - 1/math.e)
0.632120558829
In [ ]: