Kind of bored tonight so.... is this a different version?
' Function to calculate combinations (nCr)
Function Combinations(n As Integer, r As Integer) As Double
' Handle cases where r is invalid
If r < 0 Or r > n Then
Combinations = 0
' Handle base cases for combinations
ElseIf
r = 0 Or r = n Then
Combinations = 1
' Optimize calculation by choosing the smaller 'r'
ElseIf
r > n / 2 Then
Combinations = Combinations(n, n - r)
' Calculate the combination iteratively
Else
Dim result As Double
result = 1
For i = 1 To r
result = result * (n - i + 1) / i
Next i
Combinations = result
End If
End Function
Sub CalculatePairProbability()
' Declare variables to store the calculated values
Dim totalPossibleHands As Double
Dim handsWithNoPair As Double
Dim probabilityOfNoPair As Double
Dim probabilityOfAtLeastOnePair As Double
' 1. Calculate the total number of possible 5-card hands
totalPossibleHands = Combinations(52, 5)
' 2. Calculate the number of 5-card hands with NO pairs
' a) Choose 5 distinct ranks from 13 available ranks
Dim numberOfRankCombinations As Double
numberOfRankCombinations = Combinations(13, 5)
' b) For each of these 5 ranks, choose 1 of the 4 suits
Dim numberOfSuitCombinations As Double
numberOfSuitCombinations = 4 ^ 5 ' 4 choices for each of the 5 cards
' c) Total number of "no pair" hands
handsWithNoPair = numberOfRankCombinations * numberOfSuitCombinations
' 3. Calculate the probability of getting NO pairs
probabilityOfNoPair = handsWithNoPair / totalPossibleHands
' 4. Calculate the probability of getting AT LEAST ONE pair
' This is 1 minus the probability of getting no pairs
probabilityOfAtLeastOnePair = 1 - probabilityOfNoPair
' Display the result to the user
MsgBox "The probability of getting at least one pair in a 5-card hand is approximately: " & Format(probabilityOfAtLeastOnePair, "0.00%")
End Sub'
' --- End of Module Level Code ---