Results 1 to 1 of 1

Thread: Calculating probability in VB6

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Calculating probability in VB6

    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.
    Last edited by Ben321; Dec 14th, 2020 at 01:00 AM. Reason: Fixed code in the Combinations

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width