How a number turns into a deck

No deck on this site is stored anywhere. Each one is worked out from its number, every time, with a few steps of division. Here's how it goes, first with 4 cards and then with all 52.

Start with 4 cards

Take four cards: A♠ 2♥ 3♣ 4♦. They can go in 4! = 24 different orders, so number the orders 0 to 23. To find the cards for one of those numbers, repeat one move until the cards run out:

Divide the number by the factorial of one less than the cards left. Skip that many cards and take the next one. Carry the remainder to the next step.

Pick a number and follow along:

#17

    Why dividing works

    Picture all 24 orders listed by number. Once the top card is chosen, the other 3 cards can be arranged 3! = 6 ways. So the list falls into four blocks of 6: orders 0–5 start with A♠, 6–11 with 2♥, 12–17 with 3♣, and 18–23 with 4♦.

    Dividing by 6 tells you which block your number is in, which is the same as telling you the top card. The remainder tells you where you are inside that block. From there it's the same problem with one card fewer: 2! = 2 orders per choice of the second card, then 1 per choice of the third, and the last card is whatever's left.

    The steps also run backwards. Given a deck, count how many cards you skipped at each step and multiply back up, and you get its number. That's what the Find your number page does. Since every number leads to exactly one deck and every deck leads back to exactly one number, no two numbers ever share a deck.

    The skip counts are the number's digits in the factorial number system, where the place values are 3!, 2!, 1!, 0! instead of 1000, 100, 10, 1. For 17 they're 2, 2, 1, 0, because 17 = 2×3! + 2×2! + 1×1! + 0×0!. Using those digits to pick cards is called the Lehmer code.

    Now with all 52

    A full deck works exactly the same way, just with more steps. Start from the deck in sorted order (deck number 0): ♠ A to K, then ♥, ♣, ♦. Divide by 51!, then 50!, and so on down to 0!. The numbers are much bigger, but each step is the same move.

    Open this deck on the Shuffle page →
    Step Number so far Divide by Skip Take Remainder

    The deck you get