I am taking a statistical mechanics course this semester, and one of our homework sets focused on counting statistics, since they are foundational to the topic of stat mech. Because I've had a lot more exposure to probability than most other physics students, I found this a good opportunity to try and do the problem set programmatically. On top of that, I've been experimenting with various programming paradigms in Wolfram Language and wanted to try my hand (no pun intended) at writing a procedural style program that calculates this particular question. And that leads me to here and now: I wanted to share what I came up with!
probabilityRoyalFlush[] :=
Block[{heartSpace, clubSpace, diamondSpace, spadeSpace, sampleSpace,
totalCount, royalSpace, royalCount, royalFlush},
heartSpace = {2 "H", 3 "H", 4 "H", 5 "H", 6 "H", 7 "H", 8 "H",
9 "H", 10 "H", "JH", "QH", "KH", "AH"};
clubSpace = {2 "C", 3 "C", 4 "C", 5 "C", 6 "C", 7 "C", 8 "C", 9 "C",
10 "C", "JC", "QC", "KC", "AC"};
diamondSpace = {2 "D", 3 "D", 4 "D", 5 "D", 6 "D", 7 "D", 8 "D",
9 "D", 10 "D", "JD", "QD", "KD", "AD"};
spadeSpace = {2 "S", 3 "S", 4 "S", 5 "S", 6 "S", 7 "S", 8 "S",
9 "S", 10 "S", "JS", "QS", "KS", "AS"};
sampleSpace = Join@{heartSpace, clubSpace, diamondSpace, spadeSpace}
(*creates one sampleSpace of all cards with appropriate suits*);
totalCount =
Count[Subsets[Flatten[sampleSpace, 1], {5}], {_, _, _, _, _}]
(*gives all possible subsets of sampleSpace with exactly 5 elements*);
royalSpace = {10, "J", "Q", "K", "A"};
royalCount = 4(*since there are 4 suits*)*Count[
Subsets[royalSpace, {5}],
{_, _, _, _, _}];
royalFlush = royalCount/totalCount]
And here is the implementation:
N@probabilityRoyalFlush[]
(*1.53908*10^-6*)
I am personally a bit surprised at the lack of combinatorics features available in the Wolfram Language; standard decks of cards are used frequently to teach basic probability principles. I know there's a database of card graphics for the Wolfram set of cards, but can these be passed symbolically to different kinds of sorting functions to perform various counting statistics? A function like DeckOfCards[]
with some optional arguments/parameters that allow for easier grouping into suits or royals would be really cool to see from an educational standpoint.