How to really shuffle a deck of cards

When I need to shuffle a deck of poker cards in Java/Android, I use Collections.shuffle(List<?> list), of course. I’ve ever been doing this and the results seemed acceptable. But they aren’t.

As outlined in this paper, there are 52! possible unique shuffles of a 52 card poker deck. That amounts to about 2^226.

  • What do the colored bubbles mean in hierarchy viewer?
  • issue with “@id” vs “@+id” in positioning in Relative Layout
  • Calling Activity methods from Fragment
  • Android studio 2.3 canary missing 'Launch Standalone SDK Manager' option in 'SDK Manager'
  • View's getWidth() and getHeight() returning 0
  • SimpleCursorAdapter with ImageView and TextView
  • But Collections.shuffle(List<?> list) uses new Random() by default which uses a 48-bit seed and can therefore only create 2^48 unique shuffles – which is only 3.49*10^(-52) percent of all possible shuffles!

    So how do I shuffle cards the right way?

    I’ve started using SecureRandom, but is that enough, finally?

    List<Card> cards = new ArrayList<Card>();
    SecureRandom secureRandom;
    try {
        secureRandom = SecureRandom.getInstance("SHA1PRNG");
    catch (NoSuchAlgorithmException e) {
        secureRandom = new SecureRandom();
    secureRandom.nextBytes(new byte[20]); // force SecureRandom to seed itself
    Collections.shuffle(cards, secureRandom);

    Related posts:

    Error:(23, 17) Failed to resolve: junit:junit:4.12
    Android: How can I check if a device has Camera2 api features implemented or not?
    SyncAdapter- onPerformSync has no internet access
    Android up navigation for an Activity with multiple parents
    FusedLocationApi.getLastLocation always null
    PhoneGap: Is there a way to stop the keyboard from resizing the view?
  • toolbar back button functionality
  • Is it possible to use Python to write cross-platform apps for both iOS and Android?
  • Failure when attempting to install APK to the emulator
  • does ParseImageView cache ParseFile
  • Launch an animation onstart CustomViewPager
  • Android Espresso: How do I test a specific Fragment when following one activity to several fragment architecture
  • 5 Solutions collect form web for “How to really shuffle a deck of cards”

    You may only be able to get 248 different hands from a specific starting arrangement but there’s no requirement that you start at the same arrangement each time.

    Presumably, after the deck is finished (poker hands, blackjack and so on), it will be in an indeterminate order, and any one of those rearrangements will be suitable.

    And, if you’re worried about the fact that you start from a fixed arrangement each time you start your program, just persist the order when exiting and reload it next time.

    In any case, 248 is still a huge number of possibilities (some 280,000,000,000,000), more than adequate for a card game, more so when you come to a realisation that it’s limiting shuffles rather than arrangements. Unless you’re a serious statistician or cryptographer, what you have should be fine.

    Although you are using a SecureRandom, is still has a limited state. As long as that input seed has a smaller range than 52! it can not be completely random.

    In fact, SHA1PRNG is 160 bit seeded, which means it is still not random enough. Follow this link, it has a solution years ago by using a third party library called UnCommons Math.

    If you want real randomness, you could just skip pseudo random generators and go for something better like random numbers generated from athmospheric noise. offers an API to integrate random numbers generated that way into your own software.

    Stealing an answer from the article you link:

    FOR CT = 1, WHILE CT <= 52, DO

    The random number generator should be good and use a 64 bit seed that you pick unpredictably, preferably using hardware.

    How to really shuffle a deck?

    There are several shuffling techniques.

    Either (Stripping/Overhand):

    Cut the deck in two
        Add a small (pseudorandom) amount of one half to the front of the front of the other
        Add a small (pseudorandom) amount of one half to the front of the back of the other
    Do this until one hand is empty

    Or (Riffle):

    Cut the deck in two
        Set down a small (pseudorandom) portion of one half
        Set down a small (pseudorandom) portion of the other
    Do this until both hands are empty, and you have a new deck

    And there are more on top of this, as detailed in my link above.

    Regardless, there are so many combinations that even the perfect shuffling algorithm would take a machine exploring 2*10^50 unique permutations per second to finish exploring every permutation in the time the universe has existed. Modern computers are only predicted to hit 1 ExaFLOPs (1*10^18 floating point operations per second) by 2019.

    No human shuffler will explore that range of possibilities either, and you are, I believe (at the most basic level) simulating a human shuffling, correct? Would you find it likely that a croupier could shuffle an incrementally ordered deck into decreasing order in one shuffle? To split the deck with even ranks before odd, in one shuffle?

    I don’t find it unacceptable to limit yourself to a (albeit extremely) small subsection of that phase space (2^48 possible random numbers) in each shuffle, so long as you don’t continuously seed the same way etc.

    There are exactly 52 factorial (expressed in shorthand as 52!) possible orderings of the cards in a 52-card deck. This is approximately 8×1067 possible orderings or specifically: 80,658,175,170,943,878,571,660,636,856,403,766,975,289,505,440,883,277,824,000,000,000,000.
    The magnitude of this number means that it is exceedingly improbable that two randomly selected, truly randomized decks, will ever, even in the history of the Universe, be the same. However, while the exact sequence of all cards in a randomized deck is unpredictable, it may be possible to make some probabilistic predictions about a deck that is not sufficiently randomized.

    Also, it’s worth noting that Bayer & Diaconis in 1992 proved it only takes 7 good shuffles to properly randomize a deck, here is the section on it from wikipedia which has plenty links to papers discussing this.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.