且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在VBA中生成具有正态分布的随机数-运行时错误"1004"

更新时间:2023-02-10 12:09:54

您的问题描述似乎很神秘,但是如果要绕过工作表功能,则可以使用

Your problem description seems mysterious, but if you want to bypass the worksheet function, you could use the Box-Muller transform to generate your own normal random variables in pure VBA:

Function RandNorm(Optional mean As Double = 0, Optional sd As Double = 1) As Double
    Dim r1 As Double, r2 As Double, s As Double
    r1 = Rnd()
    If r1 = 0 Then r1 = Rnd() 'no danger of two zeros in a row in rnd()
    r2 = Rnd()
    s = Sqr(-2 * Log(r1)) * Cos(6.283185307 * r2) '6.28 etc. is 2*pi
    RandNorm = mean + sd * s
End Function