457,864 Members | 1,324 Online Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

# Poker Hand Probability

 P: 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 Replies

 Expert 100+ P: 469 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

 P: 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

 Expert 100+ P: 2,416 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

 P: 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

 Expert 100+ P: 849 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

 100+ P: 424 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

 Expert 10K+ P: 11,448 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 don't agree with this but I'll continue the discussion about this matter in the ongoing thread somewhere else. There should be a 'modus operandi' for answering a question by using a code fragment. Oct 27 '08 #8 