+ Reply to Thread
Results 1 to 9 of 9

Triangular Distribution using VBA

Hybrid View

pogo1 Triangular Distribution using... 02-20-2017, 12:15 AM
MarvinP Re: Triangular Distribution... 02-20-2017, 12:36 AM
pogo1 Re: Triangular Distribution... 02-20-2017, 12:55 AM
protonLeah Re: Triangular Distribution... 02-20-2017, 12:39 AM
pogo1 Re: Triangular Distribution... 02-20-2017, 01:07 AM
protonLeah Re: Triangular Distribution... 02-20-2017, 01:21 AM
MrShorty Re: Triangular Distribution... 02-20-2017, 01:23 AM
marium.k Re: Triangular Distribution... 02-11-2019, 06:11 PM
MrShorty Re: Triangular Distribution... 02-11-2019, 06:23 PM
  1. #1
    Registered User
    Join Date
    02-19-2017
    Location
    london, england
    MS-Off Ver
    2010
    Posts
    6

    Triangular Distribution using VBA

    hello,

    I'm trying to use Excel VBA to create a function sub for a triangular distribution with parameters a(min), b(mode), and c(max).
    The method is:
    • Calculate d = (b-a)/(c-a)
    • Generate a uniformly distributed random number U between 0 and 1 (with the rnd function)
    • If U<d return a+(c-a)*sqr(dU) as the random number
    • Else, return a+(c-a)*[1-sqr((1-d)*(1-U))] as the random number.


    Currently I have:

    Function Triangular(a As Single, b As Single, c As Single)
        
        Randomize
        Application.Volatile
        
        Dim d As Single
        Dim cumProb As Single
        Dim uniform As Single
        
        d = (b - a) / (c - a)
        uniform = Rnd()
        cumProb = prob(1)
    
    
        If uniform < d Then
            Triangular = a + (c - a) * Sqr(d * uniform)
        Else
            Triangular = a + (c - a) * (1 - Sqr(1 - d) * (1 - uniform))
        End If
    
    End Function
    But it doesn't seem to run on the excel spreadsheet.

    Any help is greatly appreciated
    Thanks

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,378

    Re: Triangular Distribution using VBA

    Hi pogo and welcome to the forum,

    I get an error in compiling the function as "prob(1)" is not defined. What is prob(1) supposed to be??
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Registered User
    Join Date
    02-19-2017
    Location
    london, england
    MS-Off Ver
    2010
    Posts
    6

    Re: Triangular Distribution using VBA

    Hello! thank you!

    I thought it was suppose to be a finding the cumulative probability? but now that i look at it, it does not seem like that needs to be there.

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,966

    Re: Triangular Distribution using VBA

    cumProb = Prob(1):
    1. Prob does not correspond to syntax and description of the Worksheetfunction per Microsoft.:
    PROB(x_range,prob_range,lower_limit,upper_limit)
    2. CumProb is never used in the function.
    Option Explicit
    Function Triangular(a As Single, b As Single, c As Single) As Double
        Randomize
        Application.Volatile
        
        Dim d As Single
        Dim cumProb As Single
        Dim uniform As Single
        Dim RetVal  As Single
        
        d = (b - a) / (c - a)
        uniform = Rnd()
        'cumProb = Prob(1)
    
        If uniform < d Then
            Triangular = a + (c - a) * Sqr(d * uniform)
        Else
            Triangular = a + (c - a) * (1 - Sqr(1 - d) * (1 - uniform))
        End If
    
    End Function
    
    Sub caller()
        Debug.Print Triangular(3, 4, 6)   ======> returns  5.47996471111746 
    End Sub
    Last edited by protonLeah; 02-20-2017 at 12:42 AM.
    Ben Van Johnson

  5. #5
    Registered User
    Join Date
    02-19-2017
    Location
    london, england
    MS-Off Ver
    2010
    Posts
    6

    Re: Triangular Distribution using VBA

    Function Triangular(a As Single, b As Single, c As Single) As Double
        
        Randomize
        Application.Volatile
        
        Dim d As Single
        Dim cumProb As Single
        Dim uniform As Single
        Dim RetVal As Single
        
        d = (b - a) / (c - a)
        uniform = Rnd()
    
        If uniform < d Then
            Triangular = a + (c - a) * Sqr(d * uniform)
        Else
            Triangular = a + (c - a) * (1 - Sqr(1 - d) * (1 - uniform))
        End If
    
    End Function
    when i type the function into the excel spreadsheet will it have to be

    =Triangular(3,4,6)

  6. #6
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,966

    Re: Triangular Distribution using VBA

    No, in a cell it should be cell addresses. for example:
    =Triangular(A1,A2,A3)

  7. #7
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,460

    Re: Triangular Distribution using VBA

    That would be one potential function call. You could also put any/all of those values into cells and use cell references =Triangular(A1,B1,C1) where A1 is 3, B1 is 4, and C1 is 6.

    I note that you are using Singles for the arguments and calculations. I have seen at least one case on this forum where the poster had some concerns with the way that Excel/VBA converted a result from Single to Double, since that conversion will not always be exact. I guess I just wonder if there is a reason you are using the Single data type rather than the Double data type for all variables.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  8. #8
    Registered User
    Join Date
    02-11-2019
    Location
    Mississauga, Ontario
    MS-Off Ver
    2016
    Posts
    1

    Re: Triangular Distribution using VBA

    Hi, I am currently doing the same problem and am having trouble because it keeps showing an error in the worksheet. If someone could help me, that would be great. Thanks.

  9. #9
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,460

    Re: Triangular Distribution using VBA

    @marium.k: This forum usually doesn't like it when you post your question in the thread of another member. They would prefer that you start your own thread. If this thread will help others understand your question, include a link to this thread in your post. When you start your own thread, it will probably be important to know exactly what code you are using, how you are using it, and what error you are getting.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 12-11-2016, 05:20 PM
  2. Macro help for Triangular Distributions
    By ConfusedUK in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 12-19-2015, 04:37 PM
  3. Triangular distributions for Excel
    By Fatzburger in forum Excel General
    Replies: 1
    Last Post: 06-29-2013, 11:49 AM
  4. mean and standard deviation of triangular matrix
    By lucinka in forum Excel General
    Replies: 6
    Last Post: 06-19-2012, 12:39 PM
  5. Random values from a Triangular Distribution
    By leebean337 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-03-2006, 11:44 AM
  6. triangular distribution
    By TD in forum Excel General
    Replies: 3
    Last Post: 12-23-2005, 08:35 PM
  7. Triangular Coordinates
    By Al in forum Excel Charting & Pivots
    Replies: 6
    Last Post: 01-13-2005, 01:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1