Results 1 to 3 of 3

Thread: Problem I can't get rid of with my code...

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2002
    Posts
    4

    Problem I can't get rid of with my code...

    Here's my code, and I'm curious if anyone can see find out what's wrong with it. This describes a program that can calculate probability... And if anyone wants the files I'll be happy to email them to you. Thanks! (btw, i've explained the problems at the bottom of the page)

    'The formula is P(success) = (n! / (k! * l! * (n - k - l)!)) * (p ^ k) * (q ^ l) * (r ^ (n - k - l))
    'where:
    'n # of dice rolled
    'k # of desired successes
    'l # of botches
    'p is P(success)
    'q is P(botch)
    'r is P(failure)
    ' due to constriants of human capability, I will rename the the invididual variables to reflect what they symbologically represent.

    Dim pofsuccess As Double
    Dim nfactorial, kfactorial, lfactorial, mfactorial As Integer
    Dim p, q, r As Single
    Dim dicerolled, successes, botches, failures As Variant

    Public Function nn(num As Double) As Long
    'this function calculates the factorial number for the total number of dice
    Dim count As Integer
    'counter for calculation
    Dim fact_found As Long
    'factorial calculated (NB Long data type, +/- 2,147,483,648)

    'first number in factorial sequence is 1
    fact_found = 1
    'count up from 2 to number, multiplying fact_found by each integer value
    For count = 2 To num
    fact_found = fact_found * count
    Next count
    'fact_found is value to be returned by function
    nn = fact_found
    End Function

    Public Function ll(num As Double) As Long
    'this function calculates the factorial number for botching
    Dim count As Integer
    'counter for calculation
    Dim fact_found As Long
    'factorial calculated (NB Long data type, +/- 2,147,483,648)

    'first number in factorial sequence is 1
    fact_found = 1

    'count up from 2 to number, multiplying fact_found by each integer value
    For count = 2 To num
    fact_found = fact_found * count
    Next count
    'fact_found is value to be returned by function
    ll = fact_found
    End Function

    Public Function kk(num As Double) As Long
    'this function calculates the factorial number for success
    Dim count As Integer
    'counter for calculation
    Dim fact_found As Long
    'factorial calculated (NB Long data type, +/- 2,147,483,648)

    'first number in factorial sequence is 1
    fact_found = 1

    'count up from 2 to number, multiplying fact_found by each integer value
    For count = 2 To num
    fact_found = fact_found * count
    Next count
    'fact_found is value to be returned by function
    kk = fact_found
    End Function

    Private Function mm(num As Integer) As Long
    'this function calculates the factorial number for failure
    Dim count As Integer
    'counter for calculation
    Dim fact_found As Long
    'factorial calculated (NB Long data type, +/- 2,147,483,648)

    'first number in factorial sequence is 1
    fact_found = 1

    'count up from 2 to number, multiplying fact_found by each integer value
    For count = 2 To num
    fact_found = fact_found * count
    Next count
    'fact_found is value to be returned by function
    mm = fact_found
    End Function

    Private Sub Command1_Click()
    dicerolled = (Val(n.Text))
    successes = (Val(k.Text))
    botches = (Val(l.Text))

    'the botches variable will have to be redifined as a value from 0 to (dicerolled - successes)
    'this will require removing the l.text from the field testing form.
    'such a procedure will have to wait until the basic idea of the calculation can be used

    p = Val(psuccess.Text)
    q = Val(pbotch.Text)
    r = (1 - p - q)
    'p,q, and r have not been changed into words for the sake keeping the formula from getting absurdly long.
    failures = (dicerolled - successes - botches)

    nfactorial = nn(n)
    kfactorial = kk(k)
    lfactorial = ll(l)
    mfactorial = mm(dicerolled - successes - botches)
    ' "n" "k" and "l" are used instead of their lengened named counterparts to avoid a byref type mismatch error.
    pofsuccess = (nfactorial / (kfactorial * lfactorial * mfactorial)) * (p ^ successes) * (q ^ botches) * (r ^ (dicerolled - successes - botches))
    Label2.Caption = pofsuccess

    'The formula is P(success) = (n! / (k! * l! * (n - k - l)!)) * (p ^ k) * (q ^ l) * (r ^ (n - k - l))
    'where:
    'n # of dice rolled
    'k # of desired successes
    'l # of botches
    'p is P(success)
    'q is P(botch)
    'r is P(failure)
    'From this definition, it is easy to see how I've manipulated variable names.

    End Sub

    (The problem I have is that if successes and botches are = to 0 or 1, my program crashes.)
    If it is to be, it is up to me!

  2. #2
    trophyfish
    Guest

    use the debug feature

    Use debug feature to step through your code and the guilty code will be revealed to you.

  3. #3
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Just so you know.
    VB Code:
    1. Dim pofsuccess As Double
    2. Dim nfactorial, kfactorial, lfactorial, mfactorial As Integer
    3. Dim p, q, r As Single
    4. Dim dicerolled, successes, botches, failures As Variant
    In the above declarations what you are really doing is probably not what you think you're doing.

    pofsuccess = double
    nfactorial = variant
    kfractorial = variant
    lfactrorial = variant
    mfactorial = integer

    In VB you have to decalare the type of EVERY variable or it will be a variant

    Dim x, y , z as integer

    x = variant
    y = variant
    z = integer

    This...
    Dim x as integer, y as integer, z as integer

    or this...

    Dim X as integer
    dim Y as integer
    dim Z as integer

    will make them all integers

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