How to really shuffle a deck of cards

后端 未结 5 2166
孤独总比滥情好
孤独总比滥情好 2021-02-04 08:16

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 s

相关标签:
5条回答
  • 2021-02-04 08:50

    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.

    0 讨论(0)
  • 2021-02-04 08:51

    Stealing an answer from the article you link:

    START WITH FRESH DECK
    GET RANDOM SEED
    FOR CT = 1, WHILE CT <= 52, DO
    X = RANDOM NUMBER BETWEEN CT AND 52 INCLUSIVE
    SWAP DECK[CT] WITH DECK[X]
    

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

    0 讨论(0)
  • 2021-02-04 09:03

    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.

    0 讨论(0)
  • 2021-02-04 09:06

    If you want real randomness, you could just skip pseudo random generators and go for something better like random numbers generated from athmospheric noise.

    random.org offers an API to integrate random numbers generated that way into your own software.

    0 讨论(0)
  • 2021-02-04 09:07

    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
    Repeat
    

    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
    Repeat
    

    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.
    ~Wikipedia

    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.

    0 讨论(0)
提交回复
热议问题