好吧,不是真的RANDBETWEEN()
。我正在尝试创建一个 UDF 来返回数组中数字的索引,其中数字越大,被选择的可能性就越大。
我知道如何将概率分配给工作表中的随机数(即使用MATCH()
关于概率的总和,有很多东西可以解释这一点),但我想要一个 UDF,因为我将一个特殊的输入数组传递到函数中 - 而不仅仅是一个选定的范围。
我的问题是,权重关闭,数组中后面的数字比数组中前面的数字更有可能返回,而且我看不出代码中哪里出了问题。这是到目前为止的 UDF:
Public Function PROBABLE(ParamArray inputArray() As Variant) As Long
'Takes a set of relative or absolute probabilities and ranks a random number within them
Application.Volatile (True)
Dim outputArray() As Variant
Dim scalar As Single
Dim rankNum As Single
Dim runningTot As Single
'''''
'Here I take inputArray() and convert to outputArray(),
'which is fed into the probability code below
'''''
scalar = 1 / WorksheetFunction.Sum(outputArray)
rankNum = Rnd()
runningTot = 0
For i = 0 To UBound(outputArray)
runningTot = runningTot + outputArray(i)
If runningTot * scalar >= rankNum Then
PROBABLE = i + 1
Exit Function
End If
Next i
End Function
The function should look at the relative sizes of the numbers in outputArray()
and pick randomly but weighted towards the larger numbers.
E.g. outputArray()
of {1,0,0,1}
should assign probabilities respectively of {50%,0%,0%,50%}
However when I tested that outputArray()
, for 1000 samples and 100 iterations, and graphed how frequently item 1 or item 4 in the array was returned, I got this result:
大约20%:80%分布。绘图{1,1,1,1}
(所有人都应该有平等的机会)给出 10%:20%:30%:40% 的分布
我知道我错过了一些明显的东西,但我不知道什么,有什么帮助吗?
UPDATE
有些人要求完整的代码,这里是。
Public Function PROBABLE(ParamArray inputArray() As Variant) As Long
'Takes a set of relative or absolute probabilities and ranks a random number within them
Application.Volatile (True) 'added some dimensions up here
Dim outputArray() As Variant
Dim inElement As Variant
Dim subcell As Variant
Dim scalar As Single
Dim rankNum As Single
Dim runningTot As Single
'convert ranges to values
'creating a new array from the mixture of ranges and values in the input array
''''
'This is where I create outputArray() from inputArray()
''''
ReDim outputArray(0)
For Each inElement In inputArray
'Normal values get copied from the input UDF to an output array, ranges get split up then appended
If TypeName(inElement) = "Range" Or TypeName(inElement) = "Variant()" Then
For Each subcell In inElement
outputArray(UBound(outputArray)) = subcell
ReDim Preserve outputArray(UBound(outputArray) + 1)
Next subcell
'Stick the element on the end of an output array
Else
outputArray(UBound(outputArray)) = inElement
ReDim Preserve outputArray(UBound(outputArray) + 1)
End If
Next inElement
ReDim Preserve outputArray(UBound(outputArray) - 1)
''''
'End of new code, the rest is as before
''''
scalar = 1 / WorksheetFunction.Sum(outputArray)
rankNum = Rnd()
runningTot = 0
For i = 0 To UBound(outputArray)
runningTot = runningTot + outputArray(i)
If runningTot * scalar >= rankNum Then
PROBABLE = i + 1
Exit Function
End If
Next i
End Function
开始inputArray()
???? outputArray()
部分用于标准化不同的输入方法。 IE。用户可以输入值、单元格引用/范围和数组的混合,并且该函数可以应对。例如{=PROBABLE(A1,5,B1:C15,IF(ISTEXT(D1:D3),LEN(D1:D3),0))}
(看图你就懂了)应该同样有效=PROBABLE(A1:A3)
。我循环遍历 inputArray() 的子元素并将它们放入我的 outputArray() 中。我相当确定这部分代码没有任何问题。
然后为了得到结果,我将 UDF 复制到A1:A1000
,使用了COUNTIF(A1:A1000,1)
或者不是计数 1,而是对每个可能的 UDF 输出计数 2、3、4 等并制作了一个简短的宏来重新计算工作表 100 次,每次将 countif 的结果复制到表格中以绘制图表。我无法准确地说出我是如何做到这一点的,因为我把这一切都留在了工作中,但我会在周一更新。