What I currently have:
Option Explicit
Function Triangular(a As Double, b As Double, c As Double) As Double
Randomize
Application.Volatile
Dim d As Double
Dim uniform As Double
Dim retval as Double
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
I'm having trouble in regards to creating a triangular distribution function in VBA, which calculates a random number from arguments made from:
Calculate d = ( b - a )/( c - a )
Generate a uniformly distributed random number U between 0 and 1 with VBA's Rnd function.
If U <= d, return a + ( c - a ) × sqr(d×U) as the random number. (Sqr(x) is a VBA function which returns the square root of x.
If U > d , return a + ( c - a ) × (1 - sqr((1- d )×(1-U))) as the random number.
The parameters a and c are the minimum and maximum possible values respectively, and the parameter b is the most likely value (where you see the high point in the triangle).
I'm unsure on how to create this function and was wondering if someone could lend a hand? In working on the function I realize I need to use randomize function in order to not generate similar results each time the function is called, as well as the application.volatile operation.