keithsw1111 said:
I will post some pseudocode tomorrow that shows the algorithm I came up with.
The following code is psudocode is based loosely on Java/C#. It would never compile as it assumes some functions that the coder would need to supply but it should give you an idea of the algorithm
// list_of_desired_suits is an order independent list of the suits we want the drawn cards to have. For example if we want the probability 1 or more clubs then we pass in a list with 1 club. If we want the probability of 2 or more clubs then we pass in a list with 2 clubs. If we want the probability of 5 clubs in 5 drawn cards then we pass in 5 clubs in the list. If we want at least 2 clubs and at least 2 hearts then we pass in 2 clubs and 2 hearts in the list.
// known_cards is a list of cards we know can't be in the drawn cards. Typically this might be because they are already in our hand or have already been played.
// drawn_cards is the number of cards being drawn without replacement. If must be at least as large as there are cards in the list_of_desired_suits otherwise the result is 0.0 ... impossible.
Function GetProbability(list_of_desired_suits, known_cards, drawn_cards)
{
numerator = 1; // start with a numerator of 1
result = 0.0; // start with a probability of 0
if (count(list_of_desired_suits) > drawn_cards)
{
return 0.0; // impossible
}
// reduce the desired suits to just the unique ones
list_of_unique_desired_suits = remove_duplicates(list_of_desired_suits);
// work out how many cards are in my deck but not one of the desired suits.
not_in_desired_suits = countcardsindecknotin(list_of_unique_desired_suits);
// multiply the numerator by the number of combinations of cards in each of the desired suits
foreach(suit s in list_of_unique_suits)
{
numerator = numerator * BinomialCoefficent(countcardsindeckinsuit(s), drawn);
}
// multiple the numerator by the number of combinations of cards not in any of the desired suits where the number of cards in list_of_desired_suits is less than the cards drawn.
` numerator = numerator * BinomialCoefficient(not_in_desired_suits, drawn - count(list_of_desired_suits));
// probability of exactly the number of cards of the desired suits being in the drawn cards
result = numerator / BinomialCoefficient(undealtcards, drawn);
// now we need to add in the probability that one of the unknown cards is the same suit as our desired cards
if (count(list_of_desired_suits) < drawn_cards)
{
// we need to add the probability of each of the desired suits being in the unknown cards
foreach(suit s in list_of_unique_desired_suits)
{
// create a working copy
new_list = list_of_desired_suits;
// add a demand for an extra copy of one of our desired suits
new_list.Add(s);
// recursively call this function to calculate the probability of this more specific desired suits list
result = result + GetProbability(new_list, known_cards, drawn_cards);
}
}
return result;
}