468,734 Members | 2,184 Online

# Poker Hand Probability 2
Hi, I am taking a intro to C++ course so my knowledge base only limits to areas such as if/else, functions, and recursions.

We are creating a program which will determine the probability of Poker Hands, such as Royal Flush, Straight Flush, Four of a Kind and such. However in order to complete this, I need to determine how many different poker hands there are. On pencil and paper, this is relatively easy as its a simple combination problem.

52! / 5! 47!

52 represents the deck, 5 represents the hand, and 47 represent the Deck-Hand. The total amount of poker hands are 2,598960.

The problem is that I cant seem to implement this into my program. I cannot simply pull a random number such as 2,598960 out of thin air. I must prove it within the confines of my program with the use of combinations. Thus far, I have attempted to use a recursion for the numerator and denominator. This is not successful as 50! well exceeds the any type of data type.

In real life, there is a cancellation for simplicity which results to 52x51x50x49x48/5x4x3x2x1. This would be much easier for the program to calculate. However I doubt there is a way for the program to perform cancellations.

In general, I am having a hard time initializing this program simply because I dont know how to force the program to perform such calculations. Any help is appreciated.
Oct 26 '08 #1
7 5164 boxfish
469 Expert 256MB
In my opinion, it's possible to make a computer program do anything. That's probably not very reassuring. But if you can think of an algorithm for it, you can code it. Your program should be able to preform cancellations. I can think of one way to do this now, although it would not be very efficient. Every time you multiply the result by one of the huge numbers, loop through all the numbers you're dividing it by to see if it's divisible by any of them yet If it is, divide, and then scratch that number from the list. Oh wait... You don't have a list. Has your class covered arrays or vectors yet?
Good luck.
Oct 26 '08 #2
curiously enough
79 You don't need arrays or anything like that. Cancellations can be done much more easily than you think.

First, I think you already have the recursive function that calculates the factorial of an integer. So you will use this function for the small denominator(which is 5 in your example).

You will have to write another function too, call it cancellation. It should take two parameters. The first one is the large numerator(which is 52 in you example), and the second one is the larger denomenator(which is 47 in your example). This function will be very similar to the factorial function, except for the if statement that is used in the factorial function to stop the recursivity(which is something like: if(n>1)). Instead the if statement should be: if(n>q) (or if(52>47) in your example) and that should stop the recursive multiplication at n-q (at 48 in your example). And remember to change the first parameter only, as the second parameter stays constant throughout the recursivity( meaning you should write:
if(n>q) return(n*cancellation(n-1,q)) );

And in main all you have to do is divide the return value of cancellation by the return value of factorial(where factorial takes the small denominator as parameter ofcourse).
Oct 26 '08 #3
donbock
2,422 Expert 2GB
The combination is the denominator for the probability. Don't stall out while you're waiting to figure out how to compute that denominator value -- try to compute the numerator values for various poker hands. That is, the number of distinct combinations of cards in your hand that all amount to a particular poker hand. You can manually verify a few of these to see your calculations are correct.
Oct 26 '08 #4
Extremity
2 Instead of recursion, does anyone know how to implement a factorial/permutation/combination for looping instead?

Such as 52! / 5! 47!

The algorithm seems to be n * (n - 1) * (n - 2) * (n - 3).....and so forth
Oct 26 '08 #5
Laharl
849 Expert 512MB
Recursion may be the way to go, however, keep in mind that a!/b! = (a*a-1*a-2*...*b+1) if a > b, since a! = a*a-1*a-2*...*1 and b = b*b-1*b-2*...*1.

For example, 5!/3!: (5*4*3*2*1)/(3*2*1) cancels to 5*4.

Implementing this in code is up to you, however.
Oct 26 '08 #6
arnaudk
424 256MB
As mentioned above, you'll need a function which computes the factorial down to a certain number, perm(n,r) = n!/(n-r)! = n(n-1)(n-2)...(n-(r-1)). This can be achieved with a recursive function call, for example:

< spoonfeeding code deleted >

In this way, a permutation nPr is simply perm(n,r), and a combination or binomial coefficient nCr is perm(n,r)/perm(r,1).
Oct 27 '08 #7
JosAH
11,448 Expert 8TB
You can even go a bit further if you note that a sequence of n adjacent numbers
contains one number that can be divided by n itself. So, e.g. 52C5 = 52!/5!/(52-5)!
== 52*51*50*49*48/1*2*3*4*5 == 48/1*49/2*50/3*51/4*52/5. Note that divisions
are always exact and the intermediate results are always as small as possible.
One single loop can do the job here.

kind regards,

Jos

ps. I ostensibly removed the spoonfeeding code in previous replies; I personally
 2 posts views Thread by Andreas Schmitt | last post: by 7 posts views Thread by ibtc209 | last post: by 1 post views Thread by Martin Olsen | last post: by 27 posts views Thread by Simon Biber | last post: by 4 posts views Thread by hardieca | last post: by 15 posts views Thread by sandy123456 | last post: by 13 posts views Thread by kinghippo423 | last post: by 24 posts views Thread by bnashenas1984 | last post: by 9 posts views Thread by teejayem | last post: by reply views Thread by PrototypeChain | last post: by reply views Thread by rvphilip | last post: by reply views Thread by coodingd | last post: by reply views Thread by ioana budai | last post: by 1 post views Thread by CARIGAR | last post: by 3 posts views Thread by anoble1 | last post: by 4 posts views Thread by HopkinsUni | last post: by reply views Thread by Rubens | last post: by reply views Thread by Randomdude12 | last post: by