If anyone is interested, here is my Python program for computing the probability for the case of 12 students. To change the number of students, all that's necessary is to change the first line, currently reading "N = 12".

The students are numbered from 0 to 11, since that seems a little more natural in Python. The hat is represented by a Python "set". The program prints out the probability that the last student gets stuck with his own number from the hat.

Warning: this program can take a long time to run, as described in a previous post.

Code:

N = 12 # number of students
pN = 0
# student i draws from the hat
# the probability of getting to this point is p
def draw(i, hat, p):
global pN
if not ((N-1) in hat):
return
if i == N-1:
pN += p
return
if i in hat:
nchoices = len(hat) - 1
else:
nchoices = len(hat)
for j in hat:
if i != j:
draw(i+1, hat - set([j]), p / float(nchoices))
hat = set([i for i in range(N)])
draw(0, hat, 1.0)
print pN