gpt4 book ai didi

vba - Excel UDF 加权 RANDBETWEEN()

转载 作者:行者123 更新时间:2023-12-01 18:14:46 28 4
gpt4 key购买 nike

其实不是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

该函数应该查看outputArray()中数字的相对大小,并随机选择,但对较大的数字进行加权。例如。 {1,0,0,1}outputArray() 应分别分配 {50%,0%,0%,50%} 的概率然而,当我针对 1000 个样本和 100 次迭代测试 outputArray() 并绘制数组中第 1 项或第 4 项的返回频率时,我得到了以下结果: Graph

大约 20%:80% 的分布。绘制 {1,1,1,1}(所有机会均等)给出 10%:20%:30%:40% 分布

我知道我错过了一些明显的东西,但我不知道是什么,有什么帮助吗?

更新

有些人要求完整的代码,这里是。

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 的结果复制到表格中以绘制图表。我无法准确地说出我是如何做到这一点的,因为我把这一切都留在了工作中,但我会在周一更新。

最佳答案

试试这个:

Function Probable(v As Variant) As Long
Application.Volatile 'remove this if you don't want a volatile function

Dim v2 As Variant
ReDim v2(LBound(v) To UBound(v) + 1)

v2(LBound(v2)) = 0
Dim i As Integer
For i = LBound(v) To UBound(v)
v2(i + 1) = v2(i) + v(i) / Application.Sum(v)
Next i

Probable = Application.WorksheetFunction.Match(Rnd(), v2, 1)
End Function
<小时/>

数组v本质上是您的outputArray

该代码采用 {1,0,0,1} 之类的数组并将其转换为 {0,0.5,0.5,1}(请注意 >0 开头),此时您可以按照您的建议进行 MATCH,以相等的概率获得 1 或 4

同样,如果您以 {1,1,1,1} 开头,它将转换为 {0,0.25,0.5,0.75,1}并以相同的概率返回 1、2、3 或 4 中的任何一个。

另请注意:如果将 Application.Sum(v) 的值保存在变量中,而不是对数组 v 中的每个值执行计算,则可能会更快一些.

更新
该函数现在将v 作为参数——就像您的代码一样。我还对其进行了一些调整,以便它可以处理具有任何基数的 v,这意味着您也可以从工作表中运行它:=Probable({1,0,0,1} ) 例如

关于vba - Excel UDF 加权 RANDBETWEEN(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42159763/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com