【发布时间】:2017-06-28 19:46:39
【问题描述】:
其实不是RANDBETWEEN()。我正在尝试创建一个 UDF 以返回数组中数字的索引,其中数字越大,被选择的可能性就越大。
我知道如何将概率分配给工作表中的随机数(即在概率总和上使用MATCH(),SO 上有很多东西可以解释这一点),但我想要一个 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() 中数字的相对大小,并随机选择但偏重于较大的数字。
例如。 outputArray() 的 {1,0,0,1} 应该分别分配 {50%,0%,0%,50%} 的概率但是当我测试 outputArray() 时,对于 1000 个样本和 100 次迭代,并绘制数组中第 1 项或第 4 项返回的频率,我得到了这个结果:
大约 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 等 em> 并制作了一个简短的宏来重新计算工作表 100 次,每次将 countif 的结果复制到表格中以绘制图表。我不能确切地说我是怎么做到的,因为我把这一切都留在了工作中,但我会在星期一更新。
【问题讨论】:
-
你应该把 Randomize 放在 Rnd() 行之前。
-
有趣,似乎对我有用。您如何(以及为什么)将输入转换为输出?
-
显示将
inputArray()转换为outputArray()的代码,并描述您测试退货频率的方式。这可能会导致这样的分布。 IMO 目前的代码应该可以正常工作。 -
刚刚运行了你的实验,只添加了一条语句:
outputArray = inputArray。结果为:(50118, 0, 0, 49882)。我认为您应该检查您的实验以及复制数组的方式。这是一个单一的声明。 -
(1,1,1,1) 的结果:(25024, 25138, 24898, 24940)
标签: vba excel user-defined-functions