Here some very good code:
Search Forum below for more code.
http://www.visualbasicforum.com/showthread.php?t=168296&highlight=lotto
VB:
Option Explicit
'
' Odometer-style Combination generating functions
' by MathImagics (Dr Memory) 2004
'
' SetCombination
' NextCombination
' ThisCombination
'
Dim Cwheel() As Long
Public Sub SetCombination(ByVal N As Long, ByVal K As Long, ByVal Combn As String)
ReDim token(K) As String
token = Split(Combn, ",")
If UBound(token) <> K - 1 Then Exit Sub
Dim W As Long
ReDim Cwheel(0 To K)
For W = 1 To K
Cwheel(W) = Val(token(W - 1))
If Cwheel(W) <= Cwheel(W - 1) Then Exit Sub ' invalid combn
If Cwheel(W) > N Then Exit Sub ' ditto
Next
End Sub
Public Function ThisCombination() As String
'
' Current Combination readout
'
Dim i As Long, Comb As String
Comb = Cwheel(1)
For i = 2 To UBound(Cwheel)
Comb = Comb & ", " & Cwheel(i)
Next
ThisCombination = Comb
End Function
Public Sub NextCombination(ByVal N As Long, ByVal K As Long)
' "Combination Odometer"
'
' By MathImagics: the array Cwheel contains the current
' K items combined, in increasing order.
' Each call to this sub will adjust Cwheel
' so it contains
' the NEXT combination in lex order
'
Dim i As Long
Dim j As Long
i = K
While Cwheel(i) >= N - K + i
i = i - 1 ' find rightmost wheel that allows an increment
If i = 0 Then
' wraps around (natch!)
i = 1
Cwheel(1) = 0
End If
Wend
Cwheel(i) = Cwheel(i) + 1
For j = i + 1 To K
Cwheel(j) = Cwheel(i) + j - i
Next
End Sub
------------------
VB:
Option Explicit
Function Index2Combination( _
ByVal N As Long, ByVal K As Long, ByVal CNO As Variant) As String
'
' by Dr Memory (MathImagics) May 2004
'
' N = # of values (balls)
' K = # selected to combine
' CNO = index number of combination to return
' 1 <= CNO <= Combinations(N, K) < 75 x 10^27
'
'=============================================
Dim NC, V, cCount ' Variants (Decimal)
Dim Wheel As Long, W As Long, Combo As String
NC = Combinations(N, K)
If CNO < 1 Or CNO > NC Then Exit Function
Wheel = 1
cCount = CDec(0) ' running count of combinations
For W = 1 To K - 1
Do
V = Combinations(N - Wheel, K - W) ' how many till next value?
If V = 0 Then Exit Do
If cCount + V >= CNO Then Exit Do
cCount = cCount + V
Wheel = Wheel + 1
Loop
If W = 1 Then Combo = Wheel Else Combo = Combo & ", " & Wheel
Wheel = Wheel + 1
Next
Index2Combination = Combo & ", " & Wheel + (CNO - cCount - 1)
End Function
Function Combination2Index(ByVal N As Long, ByVal K As Long, Combn As String) As Variant
'
' by Dr Memory (MathImagics) May 2004
'
' N = # of values (balls)
' K = # selected to combine
' Combn= list of values, e.g. "2, 7, 9, 23, 40, 45"
' must be in ascending order
'=============================================
ReDim token(K) As String
token = Split(Combn, ",")
If UBound(token) <> K - 1 Then Exit Function
Dim NC, wcount, Wheel() As Long, W As Long
Dim V As Long, msg As String
ReDim Wheel(K)
Combination2Index = CDec(0)
For W = 1 To K
Wheel(W) = Val(token(W - 1))
If Wheel(W) <= Wheel(W - 1) Then Exit Function
If Wheel(W) > N Then Exit Function
Next
For W = 1 To K - 1
For V = Wheel(W - 1) + 1 To Wheel(W) - 1
Combination2Index = Combination2Index + Combinations(N - V, K - W) - 1
Next
Next
Combination2Index = Combination2Index + Wheel(K) - K + 1
End Function
------------------------