Here you will find daily news and tutorials about R, contributed by over 750 bloggers.
There are many ways to follow us - By e-mail:On Facebook: If you are an R blogger yourself you are invited to add your own R content feed to this site (Non-English R bloggers should add themselves- here)

Suppose N people (and their hats) attend a party (in the 1950s). For fun, the guests mix their hats in a pile at the center of the room, and each person picks a hat uniformly at random. What is the probability that nobody ends up with their own hat?

x.max <- 40 xs <- 1:x.max dev.new(height=6, width=10) plot(xs, sapply(xs, EstimateProbability), pch=4, ylim=c(0, 0.60), main="Men with Hats: N hats uniformly assigned to N people", xlab="N", ylab="probability that nobody ends up with their own hat") lines(xs, sapply(xs, CalculateProbability), col="firebrick", lwd=2) mtext("What is the probability that nobody ends up with their own hat?") legend("topleft", "True probability", bty="n", lwd=2, col="firebrick") legend("topright", "Estimate from 10k simulations", bty="n", pch=4) savePlot("men_with_hats.png")

# The probability converges to e^-1 as N -> Inf exp(1) ^ -1

As in my earlier puzzle post, the solution is an application of the inclusion-exclusion principle. What’s fascinating about this particular puzzle is that the probability settles down not at zero or one, but instead converges to e^-1 as the number of people (and hats) grows large. I don’t know about you, but I never would have guessed.

Related

To leave a comment for the author, please follow the link and comment on their blog: mickeymousemodels.