### Monte Carlo experiment to find the probablities of Poker hands

• Previously discusses: Monte Carlo experiments

• Monte Carlo experiments can be used to obtain an approximation of probabilities of certain events using the following procedure:

• Performing the Monte Carlo experiment for many many many times and observe the outcomes.

• The probability of an event x of the Monte Carlo experiment can be approximated by:

 ``` # times that the outcome x was observed ----------------------------------------- # times that the experiment was performed ```

• A Monto Carlo experiment to find the probabilities of Poker hands

• The following Monte Carlo experiment will find an approximation for the probabilities of Poker hands:

 ``` NHands = 10000000; for ( i = 1; i <= NHands; i++ ) { shuffle the deck; cut the deck.... deal a Poker hand; if ( hand contains Royal Flush ) NRoyal++; else if ( hand contains Straight Flush ) NStraightFlush++; else if ( hand contains 4 of a kind ) N4s++; else if ... (and so on) else NHighCard++; } Print "Probability of Royal Flush = " + (double)NRoyal / NHands; ... ```

Note:

• You cannot use this expression to compute the division:

 ``` NRoyal / NHands ```

This is an integer division and computes the quotient

• To compute the fraction, we need to use a floating point division operation.

By converting one of the operands to double, the Java compiler will tranlate the / operation to a floating point division

• Java program:

 ``` /* ============================================================= Monte Carlo experiment to find the probability of Poker hands ============================================================= */ public class PokerProbab { public static void main(String[] args) { DeckOfCards a; Card[] player1 = new Card[5]; int i, j, cut, NHands; int NRoyal, NStraightFlush, NFlush, NStraight, N4s, N3s, N22s, N2s, NFullHouse, NHighCard; if ( args.length == 0 ) { System.out.println("Usage: java PokerProbab #Hands"); System.exit(1); } NHands = Integer.parseInt( args[0] ); a = new DeckOfCards(); NRoyal = NStraightFlush = NFlush = NStraight = N4s = N3s = N22s = N2s = NFullHouse = NHighCard = 0; /* ********************************************** Deal NHands Poker hands ********************************************** */ for ( i = 1; i <= NHands ; i++ ) { a.shuffle(100); // Shuffle cut = (int) (20 + 10*Math.random()); // Cut the deck for ( j = 0; j < cut; j++ ) // Deal the cut cards away a.deal(); for ( j = 0; j < 5; j++ ) // Deal a Poker hand player1[j] = a.deal(); /* -------------------------------- Check for Poker hands -------------------------------- */ if ( Poker.isFlush(player1) && Poker.isStraight(player1) ) { Poker.sortByRank( player1 ); if ( player1[4].rank() == 14 ) NRoyal++; // Ace high Straight Flush else NStraightFlush++; } else if ( Poker.is4s(player1) ) N4s++; else if ( Poker.isFullHouse(player1) ) NFullHouse++; if ( Poker.isFlush(player1) ) NFlush++; if ( Poker.isStraight(player1) ) NStraight++; else if ( Poker.is3s(player1) ) N3s++; else if ( Poker.is22s(player1) ) N22s++; else if ( Poker.is2s(player1) ) N2s++; else NHighCard++; if ( (i*10) % NHands == 0 ) System.out.println("# Hands played = " + i); } System.out.println(); System.out.println("Probability of Royal Flush ~= " + ((double)NRoyal / NHands)*100 + "%" ); System.out.println("Probability of Straight Flush ~= " + ((double)NStraightFlush / NHands)*100 + "%" ); System.out.println("Probability of 4 of a kind ~= " + ((double)N4s / NHands)*100 + "%" ); System.out.println("Probability of Full House ~= " + ((double)NFullHouse/ NHands)*100 + "%" ); System.out.println("Probability of Flush ~= " + ((double)NFlush / NHands)*100 + "%" ); System.out.println("Probability of Straight ~= " + ((double)NStraight / NHands)*100 + "%" ); System.out.println("Probability of Set ~= " + ((double)N3s / NHands)*100 + "%" ); System.out.println("Probability of 2 Pairs ~= " + ((double)N22s / NHands)*100 + "%" ); System.out.println("Probability of 1 Pair ~= " + ((double)N2s / NHands)*100 + "%" ); System.out.println("Probability of High Card ~= " + ((double)NHighCard / NHands)*100 + "%" ); System.out.println(); System.out.println(); } } ```

Output: (using 10000000 hands played)

 ``` Monte Carlo results: Actual ----------------------------------------------------------- ------------- Probability of Royal Flush ~= 3.5E-4% (0.00035%) 0.000154% Probability of Straight Flush ~= 0.00134% 0.00139% Probability of 4 of a kind ~= 0.02363% 0.0240% Probability of Full House ~= 0.14591% 0.144% Probability of Flush ~= 0.19589% 0.197% Probability of Straight ~= 0.39564% 0.392% Probability of Set ~= 2.29112% 2.11% Probability of 2 Pairs ~= 4.74622% 4.75% Probability of 1 Pair ~= 42.25744% 42.3% Probability of High Card ~= 50.30958% 50.1% ```

• Example Program: (Demo above code)

How to run the program:

 Right click on links and save in a scratch directory To compile:   javac PokerProbab.java To run:          java PokerProbab 10000 (Use a larger number to get a more accurate approximation)