Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1449

Calculating probability in VB6

$
0
0
Here's some sample code for using in VB6 that will let you calculate probabilities according to Combinations and Permutations formulas. Combinations tells you the number of ways you can pick items from a set when the order doesn't matter, and Permutations tells you the how many ways you can pick items from a set when order does matter.
Code:

Private Function Factorial(ByVal Value As Long) As Double
    Dim n As Long
    Factorial = 1
    For n = 1 To Value
        Factorial = Factorial * n
    Next n
End Function


Private Function Permutations(ByVal ItemsToPick As Long, ByVal ItemsToPickFrom As Long, ByVal RepeatsAllowed As Boolean) As Double
    If RepeatsAllowed Then
        Permutations = ItemsToPickFrom ^ ItemsToPick
    Else
        Permutations = Factorial(ItemsToPickFrom) / Factorial(ItemsToPickFrom - ItemsToPick)
    End If
End Function


Private Function Combinations(ByVal ItemsToPick As Long, ByVal ItemsToPickFrom As Long, ByVal RepeatsAllowed As Boolean) As Double
    If RepeatsAllowed Then
        Combinations = Factorial(ItemsToPickFrom + ItemsToPick - 1) / (Factorial(ItemsToPickFrom - 1) * Factorial(ItemsToPick))
    Else
        Combinations = Factorial(ItemsToPickFrom) / (Factorial(ItemsToPickFrom - ItemsToPick) * Factorial(ItemsToPick))
    End If
End Function

I got the equations for combinations and permutations from https://www.mathsisfun.com/combinato...mutations.html and simply wrote them as VB6 code to get the functions I posted above.

Viewing all articles
Browse latest Browse all 1449

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>