生成一个具有max、min和mean(平均)的随机数在visual basics - triangular dist

Generate a random number with max, min and mean (average) in visual basics - triangular dist

我正在寻找如何使用最小值 (40)、最大值 (330) 和平均值 (100) 的 3 个数字来创建三角形分布。

此代码仅给出 2 个数字之间的随机数,不包含平均值:

 z = =RAND()*(b-a)+a

我怎样才能让它成为一个三角形的距离。 ?

根据@Severin Pappadeux 更正

Function trigDist(dMIn As Double, dMax As Double, dMean As Double) As Double

    Dim dFR         As Double
    Dim bProceed    As Boolean

    bProceed = True

    '/ error checks               

    If dMIn >= dMax Then
        MsgBox "max value less than min value."
        bProceed = False
    End If

    If dMean < dMIn Then
        MsgBox "Mean is less than min value."
         bProceed = False
    End If

    If dMean > dMax Then
        MsgBox "Mean is larger than max value."
         bProceed = False
    End If


    If bProceed Then

        dFR = Rnd()

        If dFR < ((dMean - dMIn) / (dMax - dMIn)) Then
            trigDist = Sqr(dFR * (dMean - dMIn) * (dMax - dMIn)) + dMIn
        Else
            trigDist = dMax - Sqr((1 - dFR) * (dMax - dMean) * (dMax - dMIn))
        End If
    Else

       trigDist = 0
    End If

End Function

显然,@cyboashu 的回答是错误的,因为你有 MinMaxMean.

因此,您从输入中恢复 A、B、C 开始,使用 here 中均值等于 (A+B+C)/3 的事实。还不够了解VBA,多多包涵

A = Min
B = Max
C = 3.0*Mean - A - B

If A >= B Then
    print 'Error, Min is greater than Max'
End If

If C < A Then
    print 'Error, Mean is too small'
End If

If C > B Then
    print 'Error, Mean is too large'
End If

r = Rnd()

If r < ((C - A) / (B - A)) Then
    Res = A + Sqr(r * (B - A) * (C - A))
Else
    Res = B - Sqr((1.0 - r) * (B - A) * (B - C))
End If

return Res