0
$\begingroup$

enter image description here

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

enter image description here

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!

$\endgroup$
1
  • 3
    $\begingroup$ Please correct the title and type your question. $\endgroup$
    – yarchik
    Oct 15 '21 at 21:50
0
$\begingroup$

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*)
$\endgroup$
2
  • $\begingroup$ I am getting numberPair = 1013760 and NOT (1098240) $\endgroup$
    – poker
    Oct 15 '21 at 22:55
  • $\begingroup$ @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. $\endgroup$
    – yarchik
    Oct 16 '21 at 3:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.