Ruben
Ruben

Reputation: 41

vb.net combinations different

This question is about this topic: Vb.net all combinations

Question: I use this code for my application but i've got a problem. The chance exists that i have much items which has to be combinated.

But i just want to show the first 10 combinations/results.

What i want is the text to be completely unique.

so the example at my topic you see in the beginning of this question there's an ape cow deer ... example. It doesn't matter here.

but if i got something like this:

( sometimes it's even bigger )

the first 10 results are:

but they are almost the same.

i want the first 10 results to be something like this then:

Is this possible??

Upvotes: 0

Views: 172

Answers (1)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112342

Here is my solution that I converted from c# using http://www.developerfusion.com/tools/convert/csharp-to-vb/:

Dim numbers = New Integer()() { _
    New Integer() {1, 2, 3, 4, 5}, _
    New Integer() {6, 7, 8, 9}, _
    New Integer() {3, 2, 1}, _
    New Integer() {0, 9, 8, 7, 6, 5} _
}
Dim random = New Random()
Dim codes = New HashSet(Of String)()
Dim newCode As String

For resultNr As Integer = 0 To 9
    ' Try to generate random codes until a non exisiting one is found.
    Do
        Dim sb = New StringBuilder()
        For i As Integer = 0 To 3
            Dim r As Integer = random.[Next](numbers(i).Length)
            sb.Append(numbers(i)(r)).Append("-")
        Next
        sb.Length -= 1
        newCode = sb.ToString()
    Loop While codes.Contains(newCode)
    codes.Add(newCode)
    Console.WriteLine(newCode)
Next
Console.ReadKey()

Upvotes: 3

Related Questions