Derangement Calculator

Enter the number of items to find how many derangements exist.

Result

265
0.368056
6

How it is calculated

A derangement of n items is an arrangement where every item ends up in a different place from where it started. The tool builds the count !n with the recurrence !n = (n − 1) × (!(n − 1) + !(n − 2)), starting from !0 = 1 and !1 = 0. It computes the exact integer, shown with thousands separators, and the probability that a random shuffle is a derangement: P = !n ÷ n!, printed with six decimals.

Only whole numbers from 0 to 25 are accepted; a decimal, a negative number or 26 and above gives no result. The edge cases follow the convention: there is 1 way to arrange nothing (n = 0, probability 1), no way for a single item (n = 1, probability 0), and one way for two items (the swap, probability 0.5). For larger n the probability settles close to 1 ÷ e, about 0.3679.

Worked example

Six people leave their hats and none gets their own back. Build the sequence: !0 = 1, !1 = 0, !2 = 1×(0+1) = 1, !3 = 2×(1+0) = 2, !4 = 3×(2+1) = 9, !5 = 4×(9+2) = 44, !6 = 5×(44+9) = 265. Total arrangements: 6! = 720. Probability = 265 ÷ 720 = 0.368056, about 36.8%, close to the limit 1 ÷ e.

Try this example →

Common mistakes

  • Confusing !n (derangements) with n! (all arrangements). For 6 items there are 720 arrangements but only 265 derangements.
  • Expecting the probability to fall as n grows. It does not: it quickly levels off near 0.3679, so a group of 25 is hardly different from a group of 8.
  • Entering the number of pairs or groups rather than the number of items. The tool counts permutations of n distinct items; a gift exchange among 6 people is n = 6.
  • Applying it to a draw where some items are identical, or where some people are not allowed to get certain items. The count assumes all items are distinct and the only forbidden outcome is each item in its own spot.
  • Trying n = 1 and treating 0 as an error. For one item the only arrangement keeps it in place, so zero derangements is the correct answer.

About this calculator

A derangement is a permutation in which no element ends up in its original position — the classic illustration is the “hat-check problem,” where n people check their hats and every single one is handed back the wrong one. The count of derangements, written !n, follows the formula !n = n! × Σ(−1)^k/k! for k = 0 to n, and also satisfies the tidy recurrence !n = (n−1)(!(n−1) + !(n−2)).

This calculator computes !n for any number of items and the probability that a random permutation happens to be a derangement — a value that converges quickly to 1/e ≈ 36.8% as n grows. It’s a staple example in combinatorics and discrete math courses, and the underlying reasoning shows up in algorithm design and cryptography problems involving permutations with no fixed points, as well as in the well-known “Secret Santa” gift-exchange puzzle.

Was this helpful?

You can pick up to 3 reactions.

Comments (1)

  • Editorial team

    Secret Santa is a direct use. If you redraw until nobody picks their own name, the chance that one random draw works is about 0.368 for six or more people, so you should expect roughly 2.7 attempts on average. Extra rules, such as partners not drawing each other, are not covered here. Does your draw have restrictions like that? Tell us how you handle them.

Popular calculators

All Calculators