使用 VBA 从分布生成随机数到内存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14589978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:24:27  来源:igfitidea点击:

Random Number Generation to Memory from a Distribution using VBA

vbaexcel-vbarandomexcel-2007data-analysis

提问by ruya

I want to generate random numbers from a selected distribution in VBA (Excel 2007). I'm currently using the Analysis Toolpak with the following code:

我想从 VBA (Excel 2007) 中的选定分布生成随机数。我目前正在使用具有以下代码的分析工具库:

     Application.Run "ATPVBAEN.XLAM!Random", "", A, B, C, D, E, F

Where

在哪里

A = how many variables that are to be randomly generated 
B = number of random numbers generated per variable 
C = number corresponding to a distribution
         1= Uniform
         2= Normal
         3= Bernoulli
         4= Binomial
         5= Poisson
         6= Patterned
         7= Discrete 
D = random number seed
E = parameter of distribution (mu, lambda, etc.) depends on choice for C
(F) = additional parameter of distribution (sigma, etc.) depends on choice for C

But I want to have the random numbers be generated into an array, and NOT onto a sheet. I understand that where the ""is designates where the random numbers should be printed to, but I don't know the syntax for assigning the random numbers to an array, or some other form of memory storage instead of to a sheet.

但我想将随机数生成到一个数组中,而不是生成到一张纸上。我知道""is 指定随机数应打印到的位置,但我不知道将随机数分配给数组或其他某种形式的内存存储而不是工作表的语法。

I've tried following the syntax discussed at this Analysis Toolpak site, but have had no success.

我已尝试遵循此Analysis Toolpak 站点中讨论的语法,但没有成功。

I realize that VBA is not the ideal place to generate random numbers, but I need to do this in VBA. Any help is much appreciated! Thanks!

我意识到 VBA 不是生成随机数的理想场所,但我需要在 VBA 中执行此操作。任何帮助深表感谢!谢谢!

采纳答案by Edmund Moshammer

Using the inbuilt functions is the key. There is a corresponding version for each of these functions but Poisson. In my presented solution I am using an algorithm presented by Knuth to generate a random number from the Poisson Distribution.

使用内置函数是关键。除了泊松之外,这些函数中的每一个都有相应的版本。在我提出的解决方案中,我使用 Knuth 提出的算法从泊松分布生成随机数。

For Discrete or Patterned you obviously have to write your custom algorithm.

对于离散或模式化,您显然必须编写自定义算法。

Regarding the seed you can place a Randomize [seed]before filling your array.

关于种子,您可以在填充数组之前放置一个Randomize [seed]

Function RandomNumber(distribution As Integer, Optional param1 = 0, Optional param2 = 0)

    Select Case distribution
    Case 1 'Uniform
        RandomNumber = Rnd()
    Case 2 'Normal
        RandomNumber = Application.WorksheetFunction.NormInv(Rnd(), param1, param2)
    Case 3 'Bernoulli
        RandomNumber = IIf(Rnd() > param1, 1, 0)
    Case 4 'Binomial
        RandomNumber = Application.WorksheetFunction.Binom_Inv(param1, param2, Rnd())
    Case 5 'Poisson
        RandomNumber = RandomPoisson(param1)
    Case 6 'Patterned
        RandomNumber = 0
    Case 7 'Discrete
        RandomNumber = 0
    End Select

End Function

Function RandomPoisson(ByVal lambda As Integer)   'Algorithm by Knuth

    l = Exp(-lambda)
    k = 0
    p = 1

    Do
         k = k + 1
         p = p * Rnd()
    Loop While p > l

    RandomPoisson = k - 1

End Function

回答by SeanC

Why not use the inbuilt functions?

为什么不使用内置函数?

  1. Uniform = rnd
  2. Normal = WorksheetFunction.NormInv
  3. Bernoulli = iif(rnd()<p,0,1)
  4. Binomial = WorksheetFunction.Binomdist
  5. Poisson = WorksheetFunction.poisson
  6. Patterned = for ... next
  7. Discrete =
  1. 制服 = rnd
  2. 正常 = WorksheetFunction.NormInv
  3. 伯努利 = iif(rnd()<p,0,1)
  4. 二项式 = WorksheetFunction.Binomdist
  5. 泊松 = WorksheetFunction.poisson
  6. 图案 = for ... next
  7. 离散 =

-

——

select case rnd()
    case <0.1
       'choice 1
    case 0.1 to 0.4
       'choice 2
    case >0.4
       'choice 3
end select