如何使用Excel按顺序生成一系列随机分布的数字

我有一个数字,我想正常分配到15箱或细胞。 我想要15个数字按顺序排列

例:

编号分配 – 340输出:6 9 12 16 20 24 27 30 32 32 32 30 27 24 20

…是的,我的系列不是完美的分布,但目前我正在这样做,

  • 首先创build一个数字1 2 3 4 … 14 15的线性系列
  • 然后使用Norm.Dist(x,mean,standard_dev)生成一系列z分数值,其中x = 1,2,3,…,14,15
  • 然后我使用相似的三angular形比例这些值,即。 x1 / y1 = x2 / y2其中x1 = z分数; Y1 =总和(z得分); x2 =我想要的数字; Y2 = 340

有一个更好的方法吗? 因为我必须为此生成多个matrix,而且有些事情不太正确。

这里有一个碰撞和碰撞的方法,search一个独立的正态variables的随机向量,其总和落在目标总和的给定容限内,如果是这样,重新调整所有的数值,以便精确地等于总和:

Function RandNorm(mu As Double, sigma As Double) As Double 'assumes that Ranomize has been called Dim r As Double r = Rnd() Do While r = 0 r = Rnd() Loop RandNorm = Application.WorksheetFunction.Norm_Inv(r, mu, sigma) End Function Function RandSemiNormVect(target As Double, n As Long, mu As Double, sigma As Double, Optional tol As Double = 1) As Variant Dim sum As Double Dim rescale As Double Dim v As Variant Dim i As Long, j As Long Randomize ReDim v(1 To n) For j = 1 To 10000 'for safety -- can increase if wanted sum = 0 For i = 1 To n v(i) = RandNorm(mu, sigma) sum = sum + v(i) Next i If Abs(sum - target) < tol Then rescale = target / sum For i = 1 To n v(i) = rescale * v(i) Next i RandSemiNormVect = v Exit Function End If Next j RandSemiNormVect = CVErr(xlErrValue) End Function 

像这样testing:

 Sub test() On Error Resume Next Range("A1:A15").Value = Application.WorksheetFunction.Transpose(RandSemiNormVect(340, 15, 20, 3)) If Err.Number > 0 Then MsgBox "No Solution Found" End Sub 

典型的输出与这些参数:

在这里输入图像说明

另一方面,如果我将标准偏差改为1,我只是得到没有find解决scheme的信息,因为那么在目标总和的指定容差范围内得到解的概率是微乎其微的。