A Lehmer code writes any order of n things as n small numbers. Each number answers one question: of the things not placed yet, which one comes next? Shuffle the cards, then play the animation to turn the shuffle into a code. Switch direction to turn the code back into the shuffle.
ItemsUsing
Shuffle Β· tap two cards to swap them
Lehmer code Β· tap a digit to change it
Rank among all orders
Digit sum = pairs out of order
Every order
Things to notice
The digits shrink. The first digit can be 0 to nβ1, the next 0 to nβ2, and so on. The last digit is always 0, because only one card is left by then.
One code per order. Multiply the choices and you get n Γ (nβ1) Γ β¦ Γ 1 = n! codes, exactly one for each order.
Two ways to read a digit. A card's index among the available cards equals the number of cards to its right that are smaller. Those are the same cards counted two ways.
It's a number. Read the code in the factorial number system and you get the order's position in dictionary order. The code 0 0 0 0 0 is the baseline order, and 4 3 2 1 0 is the baseline order reversed. See how the factorial base counts on an odometer.
Random digits make a fair shuffle. Pick each digit uniformly at random and every order is equally likely. That's essentially the FisherβYates shuffle.