# Texas hold 'em poker game Probability task

I wrote this code below

 deck = Flatten[Table[100*i + j, {i, 1, 4}, {j, 1, 13}]];
subsetsOf52Cards = Subsets[deck , {5}];
holeCards = RandomSample[deck, 2]
deck50Cards = Cases[deck, Except[Alternatives @@ holeCards]];
hands = Flatten[Append[#, holeCards]] & /@ subsetsOf50Cards;

pair1[{___, x_, x_, y_, y_, ___} /; x != y] := False;
pair1[{ ___, x_, x_, ___, y_, y_, ___} /; x != y] := False;
pair1[{___, x_, x_, x_, x_, ___}] := False;
pair1[{___, x_, x_, x_, ___}] := False;
pair1[{___, x_, x_, ___}] := True;  (* a pair *)
pair1[{___}] := False;
pairQ[hand_] := pair1[Sort[Mod[hand, 100]]];
numberPair = Count[hands, _?(pairQ)];
numberPair/Length[  subsetsOf52Cards];
N[numberPair/Length[ subsetsOf52Cards]]
0.390064


When I want calculate the probability of one pair I am getting 0.390064 but the correct answer should be around 43 % I mean 0.43

Any suggestions would kind!

• Please correct the title and type your question. Oct 15 '21 at 21:50

You need to improve your patterns:

pair1[{___, x_, ___, x_, ___, y_, ___, y_, ___} /; x != y] := False;
pair1[{___, x_, ___, x_, ___, y_, ___, y_, ___} /; x != y] := False;
pair1[{___, x_, ___, x_, ___, x_, ___, x_, ___}] := False;
pair1[{___, x_, ___, x_, ___, x_, ___}] := False;
pair1[{___, x_, ___, x_, ___}] := True;(*a pair*)
pair1[{___}] := False;
pairQ[hand_] := pair1[Sort[Mod[hand, 100]]];


This yields

numberPair = Count[subsetsOf52Cards, _?(pairQ)]
N[numberPair/Length[subsetsOf52Cards]]
(*1098240*)
(*0.422569*)

• I am getting numberPair = 1013760 and NOT (1098240) Oct 15 '21 at 22:55
• @poker hands was not defined in your post. I had to come up with something. I used deck50Cards for that matter. Now you still have subsetsOf50Cards undefined. Oct 16 '21 at 3:07