Today I’m managing to combine multiple interests into one post; roleplaying, maths and programming. I’ve written a program that simulates poker hands, which I will post further down, but first a little background.
For the non-roleplayers; Deadlands Reloaded is a roleplaying game that is set in the Weird West and one of the mechanics involves drawing cards and attempting to make the best poker hand possible. The number of cards drawn increases as your character gains experience; a very experienced character, who has taken all the relevant advances (and is dead… don’t ask), can be drawing 14 cards to try and make a five card poker hand. Obviously, at any level, it’s good to have some idea of how likely you are to make a particular hand, given your number of cards.
For the non-mathematicians; these odds are actually pretty hard to calculate. The probabilities of getting each hand with just five cards are well known; anyone with a solid grasp of probability can grind out the numbers. However if you’re drawing a lot more cards, this is very difficult. For example, calculating the odds of getting two pairs from 12 cards is reasonably easy, but you’ve got to subtract the odds that you also have a straight (because that would override the two pair). Even that is not as simple as calculating the odds of a straight; specifically you’ve got to calculate the odds of getting a straight in a hand that also has two pairs. I’m sure this is not technically impossible, but it’s a lot of hassle, particularly as you have to do it separately for every hand size… and then add wildcard jokers on top.
So, at this point, the mathematician part of my brain switched off and the statistical programmer part of my brain switched on. I might not be able to calculate the exact odds, but simulating a few thousand hands would give me a pretty reasonable idea.
Now, it should be noted that, while I’ve been intending to learn Python for years, I’ve only actually been learning it for a couple of weeks. This program involved a lot of Googling and is probably not as efficient as it could be. However, I’m pretty confident that it at least works correctly (though if anyone spots any weird edge cases with the jokers, do let me know).
The program below (Dropbox link, as I can’t embed it here directly), when submitted, will ask for three pieces of information:
1) The number of hands you want to simulate – I’ve been running about 10,000 at a time, as that seems like a good balance between run time and statistical robustness.
2) The number of cards to draw – it suggests 5-14, as that is the maximum number a Deadlands character will realistically draw, but you can enter 54 and get a royal flush every time if you really want.
3) Whether the red joker is allowed – high level characters in Deadlands can use the red joker as a wildcard without blowing themselves up… and not blowing up is always preferable.
Feel free to use this program in whatever way you want, though if you post it anywhere else online, do me the courtesy of crediting me.
If any more experienced Python programmers want to suggest code improvements, I’m all ears. In particular, does anyone know if I really need to make the variables global to pass them from function to function? It seems inelegant, but I couldn’t get it to work otherwise.