开发者

Randomly choose a value in one column subset by another column

What is the simplest formula I can use to randomly choose a value in column A that is associated with a given B value. So in the table below, I'm looking to randomly choose an开发者_如何学Python A where B = 3. So I'm randomly choosing between row 1 (5.4) and row 3 (4.2). Note that this table can be arbitrarily large.

    A     B

1   5.4   3          
2   2.3   1
3   4.2   3
4   9.2   2
    ...   ...


Conceptually you could do it a number of ways, but here's one (VBA) where you'd use an array of possible choices then get a random element from that list:

  1. Create a udf that takes a range and the search value
  2. Loop through the row and if it equals your search value, get the value in the cell offset -1 and store it in an array
  3. Once you are done, you'll have an array of all possible answers. Use the randbetween function and give it the lbound and ubound of your array.
  4. Return the i element where i is the random number it picked.

UPDATE: Here is a code example that loops through the range for the number you specify, and if it find it, it adds the A column value to an array of possible results. Then a random number is generated and used to return a random value from that list.

Function GetRand(ByVal cell_range As Range, ByVal criteria As Double) As Double

Dim cell As Range
Dim rNum As Long
Dim i As Long
Dim possibleChoices() As Double
ReDim possibleChoices(1 To cell_range.Count)

i = 1
For Each cell In cell_range
    If cell.Value = criteria Then
        possibleChoices(i) = cell.Offset(0, -1).Value
        i = i + 1
    End If
Next

rNum = Application.WorksheetFunction.RandBetween(1, i - 1)
GetRand = possibleChoices(rNum)

End Function

Optimization: Here is a more flexible version of the same function. It takes 3 paramteres - the range you want to look in, what you want to find, and the offset value of the cell you want a random result from. It also uses Variants, so you can search for text or numbers. So in your case, you'd write:

=GetRand(B1:B5, 3, -1)

Here is the code:

Function GetRand(ByVal cell_range As Range, _
                 ByVal criteria As Variant, _
                 ByVal col_offset As Long) As Variant

Application.ScreenUpdating = False
Dim cell As Range
Dim rNum As Long
Dim i As Long
Dim possibleChoices() As Variant
ReDim possibleChoices(1 To cell_range.Count)

i = 1
For Each cell In cell_range
    If cell.Value = criteria Then
        possibleChoices(i) = cell.offset(0, col_offset).Value
        i = i + 1
    End If
Next

rNum = Application.WorksheetFunction.RandBetween(1, i - 1)

GetRand = possibleChoices(rNum)
Application.ScreenUpdating = True

End Function


Old question I know......but if you're still interested here's a formula solution assuming data in A2:B10

=INDEX(A2:A10,SMALL(IF(B2:B10=3,ROW(A2:A10)-ROW(A2)+1),RANDBETWEEN(1,COUNTIF(B2:B10,3))))

returns #NUM! error if there are no 3s in B2:B10.....or enclose in IFERROR to return text of your choosing in that case....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜