The guide
Every shuffle has an address: ranking permutations in dictionary order
Each arrangement of distinct items sits at one exact position in dictionary order. A short code, built from counts of smaller items, finds that position and runs backwards to the arrangement.
- 82 rank of 3 1 4 0 2 among 120
- 2783915460 millionth arrangement of 0 to 9
- 3:4:1:0:1:0 463 in the factorial number system
- 68 digits length of 52 factorial
- 2,568 digits in 1000 factorial
Read the full guide Fold the guide
Write the letters a, b and c in every possible order and you get six strings: abc, acb, bac, bca, cab and cba. Put them in the order a dictionary would use and each one gets a position. The string bca comes fourth, which counting from zero is position 3.
That small act hides a surprising fact. The position can be computed straight from the arrangement, without writing the list down, and the arrangement can be recovered from the position just as directly.
Counting is where people slip, and the slips are always the same. They count repeated items as if they were different. They start numbering at one in one step and zero in the next. They sort numbers as text and decide that 10 comes before 9. Each mistake is small, and each one moves the answer to a different position.
What dictionary order means for arrangements
Dictionary order compares two sequences place by place and decides at the first place where they differ. The same idea works for any items that can be put in a fixed order, not only letters. Mathematicians call it lexicographic order, and it is the plain generalisation of alphabetical order to sequences of ordered symbols.
Everything depends on that fixed order of the items, called here the base order. For letters it is the alphabet. For whole numbers it is their size, so 9 comes before 10. For mixed text it has to be some agreed rule, such as comparing code points. Change the base order and every position changes with it.
How many arrangements are there? The first place can hold any of n items, the next any of the n minus 1 that remain, and so on down to one. The product is n factorial, written n!. The sequence of factorials, 1, 1, 2, 6, 24, 120, 720, is the count of permutations of n letters. Because the list has exactly n! entries, a position counted from zero runs from 0 to n! minus 1. Counted from one it runs from 1 to n!. Neither is wrong. They differ by exactly one, and forgetting which was meant is how a correct method produces a wrong answer.
Standard libraries agree on the order. The permutations routine in Python’s itertools emits arrangements in lexicographic order of its input, so a sorted input gives a sorted listing. It also treats elements as unique by position, not by value, which is why feeding it repeated items produces repeated output.
The Lehmer code: a number hidden in every shuffle
Here is the trick. Walk along an arrangement from the left. At each place, count how many items further right are smaller than the item sitting there. Write the counts down in order. That row of counts is the Lehmer code, named for D. H. Lehmer though it was known by 1888.
The first count can be anything from 0 to n minus 1, because up to n minus 1 smaller items might lie to its right. The second is limited to n minus 2, and the last is always 0. Choices shrink by one each step, exactly like the choices that produced the n! total, so every valid row of counts matches one arrangement and no two arrangements share a row.
A worked example makes it concrete. Take 3 1 4 0 2, an arrangement of the digits 0 to 4. The 3 has three smaller items to its right: 1, 0 and 2. The 1 has one: the 0. The 4 has two: 0 and 2. The 0 and the 2 have none. The code is 3, 1, 2, 0, 0.
Now weight each count by a factorial. The first place gets 4 factorial, which is 24, the second 3 factorial, which is 6, then 2, then 1, then 1. The sum is 3 times 24 plus 1 times 6 plus 2 times 2, which is 72 plus 6 plus 4, or 82. Among the 120 arrangements of five digits, 3 1 4 0 2 has rank 82 counting from zero.
Why does the sum equal the position? Think of the dictionary. For each possible first item smaller than yours, there are 4 factorial arrangements that start with it and so come earlier. The count at the first place says how many such smaller starters there are. The same argument repeats on what remains, one place at a time.
The weights come from the factorial number system, a mixed radix scheme in which the radix changes from place to place. In it the digit with weight k factorial can be at most k. The rank written this way, with its zeros kept, is the Lehmer code itself. As the worked example on the factorial page shows, 463 is 3:4:1:0:1:0 in that system.
A bonus falls out of the same row. Adding the counts gives the number of inversions, the pairs of items out of their natural order. For 3 1 4 0 2 that is 3 plus 1 plus 2, which is 6.
Running it backwards
Unranking is the same arithmetic in reverse. Divide the position by 4 factorial. The quotient says which of the remaining items goes first, counting from the smallest, and the remainder carries on to the next place. Then divide by 3 factorial, and so on. Each time, take the chosen item out of the pool. Try the millionth arrangement of the digits 0 to 9. Counted from one it is position 1,000,000, which is 999,999 counted from zero. The successive divisions give the digits 2, 6, 6, 2, 5, 1, 2, 1, 1, 0 in the factorial system. Picking from the shrinking pool gives 2783915460, which a full listing confirms.
The same machinery gives neighbours for free. To move to the next arrangement, find the last place whose item is smaller than the one after it, swap it with the smallest larger item further right, and reverse everything after that place. The previous arrangement is the mirror image. For 3 1 4 0 2 the next is 3 1 4 2 0 and the previous is 3 1 2 4 0.
Stepping is the classic way to generate every arrangement in order, and it is considerably faster than converting each Lehmer code in turn. Ranking earns its keep when you want one arrangement far from the start, or the rank of one you already hold, with no walk at all.
Why a deck of cards outruns every list
Shuffle a deck of 52 cards. The number of possible orders is 52 factorial, about 8.0658 times 10 to the 67th, a number with 68 digits. The Lehmer code gives every one of them an exact address, but nobody can walk the list to reach it.
Suppose a machine produced a billion arrangements every second and ran for the age of the universe, about 13.8 billion years. That is roughly 4.35 times 10 to the 26th arrangements, and the deck has more than 10 to the 41st times as many. These figures are rough, but the gap is so wide that rounding cannot close it.
Exact arithmetic matters because the numbers outgrow ordinary floating point early. A double can hold every whole number exactly only up to 9,007,199,254,740,991. The factorial of 18 is below that limit and 19 factorial is above it, so a rank, which adds many large factorial terms, is not safe in ordinary floating point once the list reaches 19 items.
JavaScript has an answer in BigInt, which represents integers too large for the ordinary number type. With it, 1000 factorial is computed whole, all 2,568 digits of it, and a rank among 1000 items is just a sum of a thousand products. The work is small enough to run in a background thread, which the Web Workers interface exists to allow.
The method has firm limits. It needs every item to be different: with repeats, several orders collapse into one, the count is a multinomial coefficient rather than n factorial, and the Lehmer code stops being one to one. It ranks complete arrangements, not chosen subsets, and it will not print a list of 1000 items in every order, because nothing can.
What it offers instead is a map. Any order of any distinct items has an exact place in the whole space of possibilities, and any place gives back its order.
Sources
Every source consulted for this guide. Links checked November 10, 2026.
- Lehmer code - Wikipedia Wikipedia (CC BY-SA 4.0)
- Factorial number system - Wikipedia Wikipedia (CC BY-SA 4.0)
- Permutation - Wikipedia Wikipedia (CC BY-SA 4.0)
- Lexicographic order - Wikipedia Wikipedia (CC BY-SA 4.0)
- Inversion (discrete mathematics) - Wikipedia Wikipedia (CC BY-SA 4.0)
- Factorial - Wikipedia Wikipedia (CC BY-SA 4.0)
- Mixed radix - Wikipedia Wikipedia (CC BY-SA 4.0)
- Age of the universe - Wikipedia Wikipedia (CC BY-SA 4.0)
- DLMF: 26.14 Permutations: Order Notation NIST Digital Library of Mathematical Functions (US government)
- A000142 - OEIS The On-Line Encyclopedia of Integer Sequences (CC BY-SA 4.0)
- itertools - Functions creating iterators for efficient looping Python Software Foundation (PSF License Version 2)
- BigInt - JavaScript | MDN MDN Web Docs (Creative Commons licence)
- Web Workers API - Web APIs | MDN MDN Web Docs (Creative Commons licence)