Welcome to the board.
|
A |
B |
C |
1 |
min |
2 |
|
2 |
max |
10 |
|
3 |
mode |
4 |
|
4 |
|
6.2405 |
B4: =RandTri(B$1, B$2, B$3) |
5 |
|
5.2676 |
|
6 |
|
2.4736 |
|
7 |
|
6.2629 |
|
8 |
|
7.4321 |
|
9 |
|
5.5074 |
|
10 |
|
2.9486 |
|
11 |
|
4.4749 |
|
12 |
|
6.8287 |
|
13 |
|
4.1604 |
|
14 |
|
6.6110 |
|
15 |
|
2.8518 |
|
16 |
|
4.5164 |
|
17 |
|
4.2115 |
|
18 |
|
8.4440 |
|
19 |
|
5.2244 |
|
20 |
|
8.6486 |
|
21 |
|
6.7118 |
|
22 |
|
7.0160 |
|
23 |
|
4.6966 |
|
24 |
|
7.5159 |
|
25 |
|
6.6566 |
|
Function RandTri(a As Double, b As Double, c As Double, _
Optional bVolatile As Boolean = False) As Double
' shg 2009
' UDF or VBA
' Returns a random variate x with triangular distribution
' a <= x <= b, mode = c, a <= c <= b
' Mean = (a+b+c)/3
' E.g., =RandTr(1, 10, 7)
' http://en.wikipedia.org/wiki/Triangular_distribution
Dim U As Double
If bVolatile Then Application.Volatile
If c < a Or b < c Then
RandTri = CVErr(xlErrValue)
Else
U = Rnd()
If U <= (c - a) / (b - a) Then
RandTri = a + Sqr((b - a) * (c - a) * U)
Else
RandTri = b - Sqr((b - a) * (b - c) * (1 - U))
End If
End If
End Function
Bookmarks