: : Hi i am experimenting with an lotto program in V.S. 2008
: :
: : Lets say I got these eight numbers 2,6,12,17,18,24,29,32
: : I would like to get an array over every way to combine 7 of those
: : numbers.
: : There would be eight of them:
: : 2,6,12,17,18,24,29
: : 2,6,12,17,18,24,32
: : 2,6,12,17,18,29,32
: : 2,6,12,17,24,29,32
: : 2,6,12,18,24,29,32
: : 2,6,17,18,24,29,32
: : 2,12,17,18,24,29,32
: : 6,12,17,18,24,29,32
: :
: : For a nine numbers i would get 36: 7 number combination
: : For 10 numbers i would get 120: 7 number combinations
: : For 11 numbers i would get 330: 7 number combinations
: : For 12 numbers i would get 792: 7 number combinations
: :
: : How do i build a loop for that would iterate an array with up to
: : twelve numbers and return a new array with all possible 7 number
: : combination?
: :
: : Hope I'm making sense with my question
: :
:
: Actually, that's quite a challenging problem.
: Here's a thought:
:
: You lay out all the numbers (9 right now, but let's call it N)
: 2,6,12,17,18,24,29,32,35
:
: You want all different lotto's of length L
: And what you do each time it take N-L numbers out of this list.
: So what you need to do is find a systematic way to take N-L numbers
: out of the list. The problem reduces into finding all series
: (a,b,c,...) of length N-L where a < b < c <...
:
: In a single loop, using a dynamic array of size N-L:
:
:
: Dim NList() As Integer = {...} 'TODO: Fill list with N numbers
: Dim N As Integer = ?
: Dim L As Integer = ?
: Dim nCoords(N - L - 1) As Integer
: Dim i As Integer
: Dim bDone As Boolean = False
:
: 'Initial entry
: For i = 0 To N - L - 1
: nCoords(i) = i
: Next i
:
: Do
: 'Fix the -1's in nCoords (see below for why we need to;
: ' it won't be neccesary for the first loop)
: For i = 1 To N - L - 1
: If (nCoords(i) = -1) Then
: nCoords(i) = nCoords(i - 1) + 1
: 'As soon as we can't fix a coordinate we are done
: If (nCoords(i) >= N) Then Exit Do
: End If
: Next i
:
: 'TODO: Output lotto number by taking each element of NList() whose
: ' index is not in nCoords()
: ...
:
: 'Now for the hardest part: increment nCoords() while keeping the
: ' restriction that a < b < c < ...
: 'Start with the last coordinate
: i = N - L - 1
: Do While i >= 0
: 'Add one, but make sure we stay below N
: nCoords(i) += 1
: If (nCoords(i) >= N) Then
: 'We'll also need to increment the coordinate before this one
: 'Here we have the -1's that need fixing
: nCoords(i) = -1
: i -= 1
: Else
: 'We're done
: i = -1
: End If
: Loop
: Loop While True 'Loop will break when failing to fix coords
: :
:
: In the TODO: Print Lotto, something like this can be used:
:
:
: Dim sLine As String = ""
: Dim bAdd As Boolean
: Dim j As Integer
:
: For i = 0 To N - 1
: bAdd = True
: 'Check if 'i' is in nCoords()
: For j = 0 To N - L - 1
: If (nCoords(j) = i) Then
: bAdd = False
: Exit For
: End If
: Next j
: 'If not found, then we print this number
: If (bAdd) Then
: sLine = sLine & NList(i) & ","
: End If
: Next i
: 'Trim off the trailing ','
: sLine = Left(sLine, sLine.Length - 1)
: 'Now sLine is your Lotto
:
:
:
: I tested it (in VB6, but should work for .NET as well) and it worked
: for me. The output seemed quite ok, even though I did not test it.
:
: Good luck :)
: Best Regards,
: Richard
:
: The way I see it... Well, it's all pretty blurry
Hi
Thank you. I will try youre code above. I've found out this so far by using a bit-pattern to find out the uniqe combinations. I got help with this through another board :)
Think of it this way.
If you wanted 3 items from a LIST of 4.
If the list is as.>> a,b,c,d
Get the bit patterns.
0000
0001
0010
0011
0101
0110
0111 as b,c,d due to a being left out.
1000
1001
1010
1011 as a, c,d due to b being left out.
1101 as a,b, d due to c being left out.
1110 as a,b,c due to d being left out.
1111
Only the 4
green highlighted ones have three ones in them and each pattern is unique.
Based on the bit pattern I pick out the numbers from the LIST in the code if the digit is a 1.
Option Strict On
Imports System.IO
Imports System.Environment
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Create a LIST of numbers of TYPE Integer.
Dim aListOfNumbers As New List(Of Integer)
'Add the numbers 1 to 13 to the LIST.
For num As Integer = 1 To 13
aListOfNumbers.Add(num)
Next
'Used to hold the results.
Dim resultsList As New List(Of String)
'Get the Combinations into another LIST.
resultsList = GetCombinations(7, aListOfNumbers)
'Show the output.
Dim outputString As String = ""
For Each str As String In resultsList
outputString &= str & NewLine
Next
MessageBox.Show(outputString)
Dim response As Integer 'Used for the response.
'Ask the user if HE/She wants the results in a FILE.>>
response = MessageBox.Show("Write to a file? (Y/N)?", "File write (Y/N)?", MessageBoxButtons.YesNo)
Dim sfd1 As New SaveFileDialog
sfd1.Filter = "Text files only|*.txt"
Dim sw As StreamWriter
Try
If response = vbNo Then
Application.Exit()
Else
sfd1.ShowDialog()
sw = New StreamWriter(sfd1.FileName)
'Write the contents of the LIST to a file.
For Each st As String In resultsList
sw.WriteLine(st)
Next
sw.Close()
sw.Dispose()
'Show the results in Notepad.
Process.Start("Notepad.exe", sfd1.FileName)
sfd1.Dispose()
End If
Catch ex As Exception
MessageBox.Show("You must have clicked on CANCEL!!")
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Function GetCombinations(ByVal numberOfItems As Integer, ByVal numberList As List(Of Integer)) As List(Of String)
Dim returnList As New List(Of String)
Dim binaryStringList1 As New List(Of String)
If numberOfItems > numberList.Count Then
MessageBox.Show("Error numberOfItems must be less than or equal to the number of items in the list.")
Return returnList
End If
If numberOfItems <= 0 Then
MessageBox.Show("Error numberOfItems must be greater than or equal to 1.")
Return returnList
End If
'Generate a list of the bit patterns.
Dim string1 As String = ""
For index1 As Integer = (Power(2, numberList.Count) - 1) To 0 Step -1
string1 = Convert.ToString(index1, 2) 'Convert the number to binary.
If string1.Length < numberList.Count Then
string1 = string1.PadLeft(numberList.Count, Convert.ToChar("0"))
End If
If Has_x_Number1s_In_It(numberOfItems, string1) = True Then
binaryStringList1.Add(string1)
End If
Next
'Extract each of the numbers from the number list
'where the bit pattern is a "1" and add that sequence
'to the returnList.
Dim outputString As String = ""
For Each str As String In binaryStringList1
For index2 As Integer = 0 To str.Length - 1
If str.Substring(index2, 1) = "1" Then
outputString &= numberList.Item(index2).ToString & ","
End If
Next
outputString = outputString.Substring(0, outputString.Length - 1)
returnList.Add(outputString)
outputString = ""
Next
Return returnList
End Function
Private Function Has_x_Number1s_In_It(ByVal numberSought As Integer, ByVal aString As String) As Boolean
Dim count As Integer = 0
For index As Integer = 0 To aString.Length - 1
If aString.Substring(index, 1) = "1" Then count += 1
Next
If count = numberSought Then
Return True
Else
Return False
End If
End Function
Private Function Power(ByVal aNumber As Integer, ByVal toThePowerOf As Integer) As Integer
Dim result As Integer = aNumber
For num As Integer = 2 To toThePowerOf
result = result * aNumber
Next
Return result
End Function
End Class