array over a lotto system - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# array over a lotto system

Posts: 28Member
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

• Posts: 2,444Member
: 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:
[code]
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
[color=Blue]If (nCoords(i) = -1) Then
nCoords(i) = nCoords(i - 1) + 1
End If[/color]
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 < ...
i = N - L - 1
Do While i >= 0
[color=Blue] 'Add one, but make sure we stay below N - ((N - L - 1) - i)
' because each entry can not be bigger or equal to N, and
' each next entry is one bigger. So the restriction becomes:
'If we add 1 to this entry, make sure the last entry can still
' be below N[/color]
nCoords(i) += 1
If (nCoords(i) >= [color=Blue](L + 1 + i)[/color]) 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
'We're done as soon as the first coordinate needs fixing (= loop around)
Loop While [color=Blue]nCoords(0) <> -1[/color]
[/code]

In the TODO: Print Lotto, something like this can be used:
[code]
Dim sLine As String = ""
Dim j As Integer

For i = 0 To N - 1
'Check if 'i' is in nCoords()
For j = 0 To N - L - 1
If (nCoords(j) = i) Then
Exit For
End If
Next j
sLine = sLine & NList(i) & ","
End If
Next i
'Trim off the trailing ','
sLine = Left(sLine, sLine.Length - 1)
[/code]

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.

[color=Blue]EDIT: I had a bug in the nCoords() incrementation... I fixed it now. See the changes in Blue[/color]

Good luck
Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
• Posts: 28Member
: : 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:
: [code]:
: 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 < ...
: 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
: [/code]:
:
: In the TODO: Print Lotto, something like this can be used:
: [code]:
: Dim sLine As String = ""
: Dim j As Integer
:
: For i = 0 To N - 1
: 'Check if 'i' is in nCoords()
: For j = 0 To N - L - 1
: If (nCoords(j) = i) Then
: Exit For
: End If
: Next j
: sLine = sLine & NList(i) & ","
: End If
: Next i
: 'Trim off the trailing ','
: sLine = Left(sLine, sLine.Length - 1)
: 'Now sLine is your Lotto
: [/code]:
:
: 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
[color=Green]0111[/color] as b,c,d due to a being left out.

1000
1001
1010
[color=Green]1011[/color] as a, c,d due to b being left out.
[color=Green]1101[/color] as a,b, d due to c being left out.
[color=Green]1110[/color] as a,b,c due to d being left out.
1111

Only the 4 [color=Green]green[/color] 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.

[code]

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

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. :-)

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

End If

If Has_x_Number1s_In_It(numberOfItems, string1) = True Then

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)

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

[/code]
• Posts: 2,444Member
: 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
: [color=Green]0111[/color] as b,c,d due to a being left out.
:
: 1000
: 1001
: 1010
: [color=Green]1011[/color] as a, c,d due to b being left out.
: [color=Green]1101[/color] as a,b, d due to c being left out.
: [color=Green]1110[/color] as a,b,c due to d being left out.
: 1111
:
: Only the 4 [color=Green]green[/color] 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.
:

Yeah, I thought about that solution too. Problem is, I don't like doing 2^n permutation ... and counting the bits doesn't go that fast either.

So I thought further for a code that would loop through only exactly those permutation for which the bit-pattern restriction holds.

Ofcourse, for length 9 both codes will run fast enough. But my code will still run reasonably fast for 16, or 32 numbers.

EDIT: I did have a bug in my code though (it wasn't visible for N-L <= 2). I fixed it now - the changes are in my original post in blue

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
• Posts: 748Member
: 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

_________________________________________

Hi Eagleman,

You know who!!

In this site I have the I.d. of DrMarten.

For reference the other site thread is at.>>

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2673754&SiteID=1

Regards,

John
a.k.a DrMarten

P.S. I have not been on here in a while.