You could use a UDF:
Function aiComboByNum(ByVal n As Long, ByVal t As Long, ByVal cNum As Long) As Long()
' shg 2009-12
' VBA or Worksheet Function
' Does the opposite of iComboNum; i.e., given a combination number
' [0, Comb(n,t)-1], returns the combination.
' E.g., for 5 choose 3, the first (0th) combination is {2,1,0}, and
' the last (9th) combination is {4,3,2}
' n is not strictly necessary to the computation, but makes it quicker.
Dim ai() As Long
Dim i As Long
Dim j As Long
If cNum >= Comb(n, t) Or cNum < 0 Then Exit Function
ReDim ai(1 To t)
For i = t To 1 Step -1
Do
n = n - 1
j = Comb(n, i)
Loop While j > cNum
ai(t - i + 1) = n
cNum = cNum - j
Next i
aiComboByNum = ai
End Function
Function Comb(n As Long, t As Long) As Long
' Just like COMBIN(n, t) except t > n returns 0
Dim iDlt As Long
Dim iMax As Long
Dim i As Long
If n < 0 Or t < 0 Then Exit Function
Select Case Sgn(n - t)
Case -1
Comb = 0
Case 0
Comb = 1
Case Else
If n > 2 * t Then
iDlt = n - t
iMax = t
Else
iDlt = t
iMax = n - t
End If
Comb = iDlt + 1
For i = 2 To iMax
Comb = (Comb * (iDlt + i)) / i
Next i
End Select
End Function
To pick a random combination of 75 choose 5, select a 4-wide array of cells and array-enter this formula:
=aiComboByNum(75, 4, RANDBETWEEN(0, COMBIN(75, 4) - 1))
The first (zeroth) combination is
=aiComboByNum(75, 4, 0) = 3 2 1 0
and the last is
=aiComboByNum(75, 4, 1215449) = 74 73 72 71
Bookmarks