Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Random Combinations


Reply
 
Thread Tools Display Modes
  #1  
Old 05-27-2005, 12:09 PM
garifo garifo is offline
Newcomer
 
Join Date: May 2005
Posts: 3
Default Random Combinations


Hello Everyone,
I am new to VB.
I wanted to create a simple program maybe you guys can lead me to the right directions.

I would like to read a file that contains a list of numbers, say 20 numbers.

Once the file is read, I would like to make all combinations with those numbers. All combinations must include 4 numbers.
E.g File has the following numbers. 1,2,3,4,5,6,7,8,9,10...20
Make all postible combinations in four number format.
e.g 8, 2 6 9
1,4,7,10 etc.

Any help will be greatly appreciated. Or if anyone know where there is a free program that can do this please let me know.

Thank you
Reply With Quote
  #2  
Old 05-27-2005, 12:59 PM
bear24rw's Avatar
bear24rw bear24rw is offline
Contributor
 
Join Date: May 2004
Location: NJ
Posts: 477
Default

Code:
Dim readerFileName As String = Application.StartupPath & "\numbers.txt" Dim readerFileStream As New System.IO.FileStream(readerFileName, IO.FileMode.OpenOrCreate, IO.FileAccess.Read, IO.FileShare.Read) Dim myReader As New System.IO.StreamReader(readerFileStream) Dim writerFileName As String = Application.StartupPath & "\combos.txt" Dim writerFileStream As New System.IO.FileStream(writerFileName, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite, IO.FileShare.None) Dim myWriter As New System.IO.StreamWriter(writerFileStream) Dim numbers As New ArrayList While myReader.Peek <> -1 numbers.Add(myReader.ReadLine) End While For A As Integer = 0 To numbers.Count - 1 For B As Integer = 0 To numbers.Count - 1 For C As Integer = 0 To numbers.Count - 1 For D As Integer = 0 To numbers.Count - 1 myWriter.WriteLine(numbers(A) & "," & numbers(B) & "," & numbers(C) & "," & numbers(D)) Next Next Next Next myWriter.Flush() myWriter.Close() myReader.Close() MessageBox.Show("DONE!")
in the numbers.txt file...

1
2
a
b
c
3
4
5
6
7
...

each number on a new line, works good
Edit: Updated code...

Edit: You can now also use letters if you like
__________________
Use[vb][/vb] tags when posting code.

Last edited by bear24rw; 05-27-2005 at 01:22 PM.
Reply With Quote
  #3  
Old 05-27-2005, 01:29 PM
garifo garifo is offline
Newcomer
 
Join Date: May 2005
Posts: 3
Default

Than you for your reply...I will give it a try.


Thanks
Reply With Quote
  #4  
Old 05-27-2005, 03:06 PM
jo0ls jo0ls is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Feb 2005
Location: London
Posts: 1,050
Default

Hmm, my way was more nuts.
Say the numbers in the list are written on balls in a bag.
To get one combination you can either: draw ball 1, draw ball 2, draw ball 3, draw ball 4 = your four numbers
Or you can: draw ball 1, write down number, put it back in bag, shake, draw ball 2, write down number, put it back in bag, shake, draw ball 3, wirte down number ,put it back in bag, shake, draw ball 4 and write down number.

bear24rw's answer above assumes you put the balls back in after writing the number down, which is why you will get 1,1,1,1 ; 2,2,2,2 etc listed in the combos.

To work out the combos for the other situation I did this:
(mainly bear24rw's code)

Code:
Dim readerFileName As String = Application.StartupPath & "\numbers.txt" Dim readerFileStream As New System.IO.FileStream(readerFileName, IO.FileMode.OpenOrCreate, IO.FileAccess.Read, IO.FileShare.Read) Dim myReader As New System.IO.StreamReader(readerFileStream) Dim writerFileName As String = Application.StartupPath & "\combos.txt" Dim writerFileStream As New System.IO.FileStream(writerFileName, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite, IO.FileShare.None) Dim myWriter As New System.IO.StreamWriter(writerFileStream) Dim numbers As New ArrayList While myReader.Peek <> -1 numbers.Add(myReader.ReadLine) End While Dim i, j, num, numbers_selected As Integer Dim draw(3) As Integer For i = 0 To (2 ^ (numbers.Count)) - 1 num = i numbers_selected = 0 For j = (numbers.Count - 1) To 0 Step -1 If (2 ^ j) <= num Then numbers_selected += 1 ' got one of our four numbers num = num - (2 ^ j) If numbers_selected > 4 Then Exit For ' too many numbers picked 'hold the drawn numbers in an array: draw(numbers_selected - 1) = j If num = 0 Then If numbers_selected = 4 Then myWriter.WriteLine(numbers(draw(0)) & ", " & _ numbers(draw(1)) & ", " & numbers(draw(2)) & _ ", " & numbers(draw(3))) End If Exit For End If End If Next Next myWriter.Flush() myWriter.Close() myReader.Close() MessageBox.Show("DONE!")

edit:
whilst asleep I figured out that this code doesn't list the extra combo's you get if the draw order is important. Say the balls are numered 1 - 20;
the code will list the combo:
1234
and will not list:
1243
1423
4123
2341
2314
2134
etc.

It's like saying you reach into the bag and draw "four balls at once" instead of paying attention to the order as you draw the balls out.

Last edited by jo0ls; 05-28-2005 at 01:33 AM.
Reply With Quote
  #5  
Old 05-27-2005, 03:38 PM
garifo garifo is offline
Newcomer
 
Join Date: May 2005
Posts: 3
Default

Thanks to both of you...

jo0ls your solution is what I was after...sorry I did not make it clear.

I tried it and it worked like a charm.

Thank again.
Reply With Quote
  #6  
Old 05-31-2005, 06:15 AM
jo0ls jo0ls is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Feb 2005
Location: London
Posts: 1,050
Default

An explanation (I'm a newbie and was told off for just posting the code )

Lets say you want all the combinations for drawing 3 balls from a bag.
Here they are:
Ball num: 1 2 3
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1


This representation of the combinations is just a binary count from 0 to 2^3. So for n balls you can count in binary from 0 to 2^n to get a list of the combinations.

One way to convert a decimal number to binary notation is like this:
1)find the highest power of two that is smaller than the decimal number.
2)subtract that power of two from the number.
3) repeat from 1 with the remainder as the decimal number.

So for 50:
highest power of two is 32. remainder 18
highest power of two is 16. remainder 2
highest power of two is 2. remainder 0
Write out the binary using a 1 for the powers of two you found:
110010

Combining these two bits of information lets you list the 4-item combinations from the list of 20 items in the file.

1) count from 0 to 2^20
2) convert to binary.
3) if the binary pattern has four 1's then it is a valid combination - add it to the output
Reply With Quote
  #7  
Old 05-31-2005, 07:52 AM
wayneph's Avatar
wayneph wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

That seems like a lot of extra work... Why can't you just alter the loops so that it naturally avoids repeats...

Code:
For A As Integer = 0 To numbers.Count - 4 For B As Integer = A + 1 To numbers.Count - 3 For C As Integer = B + 1 To numbers.Count - 2 For D As Integer = C + 1 To numbers.Count - 1 myWriter.WriteLine(numbers(A) & "," & numbers(B) & "," & numbers(C) & "," & numbers(D)) Next Next Next Next

while i haven't tested it, by starting above the previous number, and not going all the way to the end, any duplicates should automaticlly weed them selves out. The first loop will go from 0-16, the second loop from 1-17, the third loop from 2-18 and the last loop from 3-20. Each time through the outer loop the starting indexes will all be incremented accordingly.
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned

Last edited by wayneph; 05-31-2005 at 11:59 AM.
Reply With Quote
  #8  
Old 05-31-2005, 10:34 AM
jo0ls jo0ls is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Feb 2005
Location: London
Posts: 1,050
Default

yeah that works (with a correction)

Code:
For A As Integer = 0 To numbers.Count - 4 For B As Integer = A + 1 To numbers.Count - 3 For C As Integer = B + 1 To numbers.Count - 2 For D As Integer = C + 1 To numbers.Count - 1
Reply With Quote
  #9  
Old 05-31-2005, 11:59 AM
wayneph's Avatar
wayneph wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

Good Catch. I've updated my post in case someone just sees the one and doesn't finish reading the thread. I was typing fast this morning and missed it...
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->