Search code examples
matlabpoker

Determining probability of 4 of a kind in a 5 card poker hand Matlab


I am supposed to determine the probability of 4 of a kind in a 5 card poker draw using Matlab. I understand the first thing I have to do is generate a deck and shuffle the cards, then draw 5 cards. I am having trouble with determining whether the hand is 4 of a kind or not. I have written the code below, which works for shuffling the deck and drawing 5 cards. I have tried to use an if statement to determine if the hand is a 4 of a kind or not, but it does not work. My reasoning behind the if statement was that if I already had a sorted vector, the only two possibilities would be the first 4 or the last 4 numbers should all equal each other

Ex. AAAA_

_2222

Any advice on how to determine 4 of a kind would be very helpful :)

DECK = ['AH';'2H';'3H';'4H';'5H';'6H';'7H';'8H';'9H';'TH';'JH';'QH';'KH'; ...
    'AS';'2S';'3S';'4S';'5S';'6S';'7S';'8S';'9S';'TS';'JS';'QS';'KS'; ...
    'AD';'2D';'3D';'4D';'5D';'6D';'7D';'8D';'9D';'TD';'JD';'QD';'KD'; ...
    'AC';'2C';'3C';'4C';'5C';'6C';'7C';'8C';'9C';'TC';'JC';'QC';'KC'];
%deck of 52 cards
total_runs=10000;
n=0;
for i=1:total_runs
  index=randperm(52);
  shuffle=DECK(index);
%shuffles the 52 columns 
  b=shuffle(1:5);
%chooses the first 5 cards
d=sort(b);

 if d(1)==d(2)==d(3)==d(4)||d(2)==d(3)==d(4)==d(5)
   %attempt to determine 4 of a kind
   disp(d);
   n=n+1;
 end
end
 prob=n/total_runs

Solution

  • You can't chain comparisons like that. You wrote:

    d(1)==d(2)==d(3)==d(4)
    

    But d(1) == d(2) evaluates to a logical, either true or false. That won't be equal to d(3).

    Since they're sorted, you can just test

    d(1)==d(4) || d(2)==d(5)