|
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:
|
/* =============================================================
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%
|
How to run the program:
|