2
$\begingroup$
  ClearAll["`*"]
    
    deck = Sort[
      Join[Range[102, 114], Range[202, 214], Range[302, 314], Range[402, 414]]]
    Length[deck]
    
    holeCards = 
     Sort[RandomChoice[deck, 2]] (*selects two random cards from our deck*)
    
    communityDeck = 
     DeleteCases[deck, 
      Alternatives @@ 
       holeCards] (*creates a new deck and removes the holeCards*)
    Length[communityDeck]
    
    orderQ[card1_, card2_] := card1 < card2;
    valueSort[hand_] := Sort[hand, orderQ]
    
    cohands = Subsets[communityDeck, {5}] // Map[Join[#, holeCards] &]
    sortedHands = valueSort /@ cohands
    
    (* A pair *)
  
    pair0Q[{___, x_, x_, ___}] := True;(* a pair*)
    pair0Q[{___, x_, x_, x_, ___}] := False;
    pair0Q[{___, x_, x_, ___, y_, y_, ___} /; x != y] := False;
    pair0Q[{___}] := False;
    pairQ[hand_] := pair0Q[Sort[Mod[hand, 100]]]
    
    Count[sortedHands, _ ?(pairQ)]/Length[sortedHands]
    
    (* Two Pairs *)
    
    twoPairQ[{___, x_, x_, ___}] := False;
    twoPairQ[{___, x_, x_, x_, ___}] := False;
    twoPairQ[{___, x_, x_, ___, y_, y_, ___} /; x != y] := True;(*two pairs*)
    twoPairQ[_] := False;
    pair2Q[hand_] := twoPairQ[Sort[Mod[hand, 100]]]
    
    Count[sortedHands, _ ?(pair2Q)]/Length[sortedHands]
    
    (* Straight Flush *)
    
    straightFlusQ[h_] := 
     MatchQ[h - h[[1]], {0, 1, 2, 3, 4, _, _}] || 
      MatchQ[h - h[[2]], {_, 0, 1, 2, 3, 4, _}] || 
      MatchQ[h - h[[3]], {_, _, 0, 1, 2, 3, 4}]
    
    Count[sortedHands, _ ?(straightFlusQ)]/Length[sortedHands]
    
    (* Flush *)
    
    

This is my code. The deck has 52 cards in total. 2 cards are randomly chosen and act as holecards. the deck is returned in subsets of 5 for all possible combinations. the holecards are inserted back into each subset of 5 becoming subsets of 7. How do I write a code for the flush which is 5 random cards of the same suit?

$\endgroup$
4
  • 1
    $\begingroup$ RandomChoice[deck, 2]] can return duplicates, use RandomSample. Also this works sortedHands = Sort /@ cohands $\endgroup$ Sep 22 at 19:52
  • $\begingroup$ Suits are being encoded as the first digit, you can extract it e.g. IntegerDigits[310] // First gives 3, and count the number of occurrences. {102, 113, 203, 302, 305, 306, 412} // Map[IntegerDigits /* First] // Counts. $\endgroup$ Sep 22 at 20:06
  • $\begingroup$ How does your set-up which involves 5 of 7 cards differs from what is described at en.wikipedia.org/wiki/Poker_probability ? I assume that the 5-car flush is based on the optimal selection of 5 from 7 cards. $\endgroup$
    – JimB
    Sep 23 at 21:12
  • $\begingroup$ Your code seems to have the objective of finding the probability of a resulting poker resulting (a pair, straight flush, flush, etc.) given the two particular hole cards selected. Is that what you want? That's different from the probability of obtaining a flush with 7 cards. $\endgroup$
    – JimB
    Sep 24 at 3:18
1
$\begingroup$

A faster option than the one in the comment

flushQ[hand_] := MatchQ[hand/100 // Floor, {___, x_, x_, x_, x_, x_, ___}]

Count[sortedHands, _?flushQ]/Length[sortedHands]
(* 20889/1059380 *)
$\endgroup$

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.