2

I'm writing a blackjack game, and just finished setting up my card and deck representations. Because Clojure doesn't have a deterministic shuffle function, I ended up piggy-backing onto Java's shuffle, but I'm concerned about the results.

Many of the test decks I've generated and shuffled contain 3+ consecutive numbers, so I'm wondering if this is to be expected, or if I should force it to shuffle the decks more than once.

An example with 3:

2 A K 3 9 J 3 8 8 6 Q 4 A 4 K J 9 5 3 8 5 J 2 4 7 K 6 8 9 2 5 4 9 A Q 2 Q 6 7 10 10 7 5 7 Q J 3 K 10 10 6 A

No, I'm not falling for the same thinking as Apple shuffle users. I know that in a perfectly random pick of values from a list with repeats, I'll expect that I may have consecutive draws.

I'm wondering though, in a shuffled deck, how many consecutive pairs should I expect? If most of my shuffled decks end up having 3+ consecutive pairs, does that suggest that the shuffling algorithm is flawed?

This has been asked before, but it didn't get any results, so I figured I'd ask again.

  • How does it work? Do you move them around at random (simulating a human shuffle) or do you select them one by one at random, and put them into a list in a (therefore) random order - I used the second one – Cato Oct 03 '16 at 17:16
  • @AndrewDeighton Like I said, it relies on Java's native java.util.Collections/shuffle method, so it depends on that. If may vary by collection type for all I know. – Carcigenicate Oct 03 '16 at 17:26

1 Answers1

2

For $i$ from 1 to 51, the chance that the $i^{\rm th}$ and $(i+1)^{\rm st}$ cards have the same value is $3/51$. So the expected number of consecutive cards with the same value is $E(X)=\sum_{i=1}^{51} (3/51)=3.$

  • Ha! So it's right on? Awesome. How did you get that the chance is 3/51 though? I'm not understanding the reasoning there. – Carcigenicate Oct 03 '16 at 12:43
  • Pick any card first. Now randomly select the second card: there are 51 cards left and only 3 of them have the same value. –  Oct 03 '16 at 12:44
  • Oh! That makes perfect sense. Thank you. – Carcigenicate Oct 03 '16 at 12:44
  • Wait. I was trying to generalize this in a function for kicks, and I realized, isn't the expected number of consecutive pairs just items-per-value - 1; assuming every value has the same number of items? So if there was a fifth suit, would I expect 4 consecutive pairs? – Carcigenicate Oct 03 '16 at 22:57
  • @Carcigenicate Not only that, it doesn't depend on the number of suits. Take a deck that consists of only 4 aces, shuffle them, and draw them out one at a time. How many consecutive pairs with the same value will you get? Three! –  Oct 03 '16 at 23:16