4

I would like to know if you take a well shuffled deck of $52$ cards and then deal out all of them one at a time without replacement, what is the probability that they will be dealt such that there is exactly one quad dealt in order such as $4$ consecutive kings? That run of $4$ has to be the only run of that length, there cannot be any others. For example, 5, ... ,K,K,K,K,7,J,J,J,J is a "loser". Other than computer simulation I am not sure how to solve this.

joriki
  • 238,052
David
  • 1,702
  • Also for those of you running a simulation like me, one easy way to test if it is working is to force there to be exactly one run of $4$ to see if the program reports a "winner" and then force there to be $2$ runs of $4$ to see if the program reports a "loser". If those $2$ conditions pass then you can "turn it loose". – David Mar 17 '16 at 13:18
  • Also note that you have to not count ANY multiple runs of $4$. There can be up to $13$ of them in the same hand although that would be extremely rare. But $2$ runs is not so rare and $3$ is certainly possible (such as JJJJ, 5, QQQQ, 7, KKKK , 8,...) – David Mar 17 '16 at 15:35
  • I did a quick tweak to my program to speed it up some and am re-running the simulation for $1$ million decisions to get a feel for how long 1 billion will take. Okay it is done is about $160$ seconds for $1$ million so that is about a $10$% speedup but will still take about $44$ hours for $1$ billion so I will have to tweak it more before I attempt that many hands. – David Mar 17 '16 at 15:51

4 Answers4

4

This can be done by inclusion-exclusion. We have $13$ conditions: the existence of a run of one of the $13$ card values. The number of configurations in which at least $k$ particular of these conditions are fulfilled is $(52-3k)!(4!)^k$, since we can consider the runs of $4$ as atoms in shuffling the cards, thus reducing the number of items to be shuffled by $3$ per run, and then we have $4!$ permutations within each run.

There are $\binom{13}k$ ways to select $k$ particular conditions, so by generalized inclusion-exclusion there are

$$ \sum_{k=1}^{13}(-1)^{k-1}\binom k1\binom{13}k(52-3k)!(4!)^k $$

configurations with exactly one run. Dividing by the total number $52!$ of configurations yields the probability

$$ \frac{2667831221357360267232187982386755586}{1136785646174010387823491412456845703125}\approx0.00234682 $$

of getting exactly one run. Since multiple runs are unlikely, the result is already quite well approximated by the first term,

$$ \frac{13\cdot49!\cdot4!}{52!}=\frac{13\cdot24}{52\cdot51\cdot50}=\frac1{425}\approx0.00235294\;. $$

joriki
  • 238,052
  • My simulation is showing a different answer so far, much higher probability. I was hoping someone else could chime in with simulation results or some other mathematical formula to help validate or invalidate this answer. Maybe my program is wrong I will check it but I am seeing a lot more frequent than just $1$ out of $10,000$ roughly. – David Mar 17 '16 at 14:35
  • @David: I just realized I missed a factor -- I'll correct the answer. – joriki Mar 17 '16 at 14:43
  • I will check it first and if I cannot find any bugs or someone else gets similar results as I do, then I can assume it is likely correct. – David Mar 17 '16 at 14:43
  • @David: Sorry about the mistake -- I've corrected the answer -- does it match your simulations now? – joriki Mar 17 '16 at 14:48
  • Yes I ran $1$ million hands and got $1$ out of about every $421$ (on average) so it looks almost identical to yours now. That is much higher probability than I would have expected for a single run of quads. I am running another $1$ million hands to get a better average. – David Mar 17 '16 at 14:49
  • I am running $10$ million hand simulation now to check for a better average. Hopefully I will get closer to $1$ out of $425$. I will need about $23,529$ winners out of $10,000,000$ – David Mar 17 '16 at 15:14
  • @David: You'll need about a billion trials to significantly differentiate between the exact result (about $1$ in $425.5$) and the approximate result that neglects double runs ($1$ in $425$). :-) – joriki Mar 17 '16 at 15:17
  • My computer is too slow for that. It will take about $1/2$ hour just to run $10$ million trials so $1$ billion would be $50$ hours. This is an interpreted language and I only have a dual core CPU $3.06$ + $3.06$ Ghz but it only shows $50$% CPU usage when running the simulation. – David Mar 17 '16 at 15:23
  • I got $23,914$ out of $10,000,000$ so that is about $1$ out of $418$ . Close but not as close as I hoped to $425.5$. – David Mar 17 '16 at 15:29
  • @David: That's a bit suprising; the standard deviation is $\sqrt{p(1-p)/n}\approx\sqrt{p/n}\approx\sqrt{1/(425n)}$ so with $10000000$ trials your standard deviation should be about $0.000015$, so at $0.0023914$ you're about $2.7$ standard deviations off. I wrote Java code to do the simulation; it should take about two hours to do the billion. – joriki Mar 17 '16 at 15:39
  • Ah, it's done -- I got a factor of $10$ wrong in estimating the two hours :-) The result is 0.002346041, and the standard deviation should be about $0.0000015$, so it's off by about $2.5$ standard deviations from the exact result and by about $4.5$ standard deviations from the approximate result. – joriki Mar 17 '16 at 15:47
  • @David: Too bad you don't seem to be around anymore. I found a small error in this -- I'd calculated the probability for at least one run instead of exactly one run. I've corrected the answer accordingly; it's now $0.00234682$, in much better agreement with my simulation result of $0.002346041$; the difference is about half a standard deviation. – joriki Sep 08 '18 at 10:16
  • I am back. Glad you fixed your slight error. – David Sep 21 '18 at 03:04
  • @David: Ah, nice, thanks for letting me know! :-) – joriki Sep 21 '18 at 03:47
3

Joriki provides the elegant solution ... but also shows the value of checking theoretical work via simulation. The discussion got me playing with simulating it in Mathematica, and thought I would post some code.

A pack of cards can be represented by the set:

cards = Table[Range[1, 13], 4] // Flatten 

Sampling without replacement, and dealing out a hand of 52 cards i.e. RandomSample[cards] ... and then counting all the cases where there are four identical cards in a row {x_,x_,x_,x_}, and doing it 10 million times:

4Row = ParallelTable[Count[Split[RandomSample[cards], SameQ], {x_,x_,x_,x_}], {10^7}];

All done. The empirical distribution is given by:

Tally[4Row]

{{0, 9976557}, {1, 23415}, {2, 28}}

In other words, out of 10 million drawings, there was:

  • exactly 1 sequence of 4 identical cards 23,415 times (out of 10 million)

  • exactly 2 sequences of 4 identical cards just 28 times (out of 10 million)

The above Monte Carlo simulation (10 million times) takes about 42 seconds on my Mac. I am sure there are faster ways of doing it, but that's another question :)

wolfies
  • 5,174
1

It seems that your shuffling algorithm is too slow. For the first card, you can pick a number n between 1 and 52. If it's 1, your card is where it's supposed to be. Otherwise, swap cards 1 and n. Then for card 2, pick a number between 2 and 52... While swapping seems to double the amount of movement, it means that the card to be chosen comes from a tight array so you don't have to search for it and this will make shuffling much faster.

For detecting 4 in a row, my idea is like punching holes in data cards so that if light shines through 4 cards, it's a match. The digital analog to this is setting a bit according to the rank of the card and ANDing four consecutive values together to see if one bit remains set.

Processed a billion simulations in 344 seconds with 1, 2, and 3 matches occurring with frequency 2347407, 3090, and 6 respectively.

program four
   implicit none
   real harvest(51)
   integer i, j
   integer, parameter :: N(51) = [(i,i=52,2,-1)]
   integer indices(51)
   integer matches(13)
   integer jmax
   integer cards(52)
   integer, parameter :: init(52) = [((ishft(1,j),j=0,12),i=1,4)]
   integer match
   integer(selected_int_kind(18)) t0,tf,rate

   matches = 0
   jmax = 1.0e9
   call random_seed()
   call system_clock(t0)
   do j = 1, jmax
      call random_number(harvest)
      indices = harvest*N
      cards = init
      do i = 1, 51
         if(indices(i) /= 0) cards([i,indices(i)+i]) = cards([indices(i)+i,i])
      end do
      match = 0
      do i = 1, 49
         if(iall(cards(i:i+3)) /= 0) match = match+1
      end do
      if(match > 0) matches(match) = matches(match)+1
   end do
   write(*,'(a,1x,i0)') 'Number of simulations = ',jmax
   write(*,'(a)') ' N Frequency'
   do i = 1, 13
      write(*,'(i2,1x,i0)') i , matches(i)
   end do
   call system_clock(tf)
   call system_clock(count_rate=rate)
   write(*,'(a,1x,g0)') 'Time in seconds = ', real(tf-t0)/rate
end program four
user5713492
  • 15,938
  • Okay I will try a faster shuffling algorithm when I have more time and when I get it fast , I will try the $1$ billion hands. It has to run in no more than $24$ hours so I will test first with $1$ million which has to run in $86$ seconds or less so that $1$ billion will run in $86,000$ seconds or less with $86,000$ seconds being about 24 hours. – David Mar 18 '16 at 02:57
  • 1
    I did 2 dozen simulations of 1.0e9 while watching the news. For 1 match, $\bar x = 2346762$, $s = 1621$, 2 matches: $\bar x = 3051$, $s = 48.26$, 3 matches: $\bar x = 2.708$, $s = 1.57$. Never hit 4 matches. Running 4 at once gets a throughput of about 1 per 1.7 minutes. – user5713492 Mar 18 '16 at 05:30
0

I wanted to post my algorithm for those that may want to attempt to simulate this in any general purpose language. These are the major steps . Some minor details are left out for simplicity here...

Step $1$: declare and initialize an array A of size $52$ such that A[$1$] = $1$, A[$2$] = $2$... A[$52$] = $52$.

Step 2: declare an initialize and array F[$52$] of flags to false so we know which cards are already drawn/picked. Change the appropriate flag to true when a card is picked (such as A[$22$] = true) if random number generator gives us $22$.

Step 3. Convert all $52$ cards to ranks only ($1$ to $13$)

Step 4: Count up runs of $4$. I "cheat" when I do this by converting the cards to a string such as "K2J7777A5..." then I just scan for AAAA, 2222, 3333... KKKK. If I detect exactly one set of quads it is a winning hand.

My first tweak is I only choose $51$ cards randomly and then just assign the one unchosen card as the last. That sped things up by about $10$%.

My next tweak is to try a faster way to choose all $52$ cards (such as simulate a shuffle). The language I am using has an array element delete function that will compact the remaining array so I can pick smaller and smaller random numbers that way but I don't yet know if it is faster.

David
  • 1,702