Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have written a code that generates all possible permutation with repetition of a string, I want to make shorter and much faster kind of this program but I don't know how to
Btw, This is my code of my program

Imports System.Text

Public Class Form1

    Private blkRpt As Integer
    Private perm As Double

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim textlen As Double = TextBox1.Text.Length
        Dim rtb As Double = TextBox2.Text
        perm = textlen ^ rtb
        permutate()
    End Sub

    Private Sub permutate()

        For a As Integer = 0 To 1 Step 0
            Dim s As String = TextBox1.Text
            Dim len As Integer = TextBox2.Text
            Dim r As New Random
            Dim sb As New StringBuilder

            For i As Integer = 1 To len
                Dim idx As Integer = r.Next(0, s.Length)
                sb.Append(s.Substring(idx, 1))
            Next

            Dim sring As String = sb.ToString

            If RichTextBox1.Text.Contains(sb.ToString) Then
                blkRpt = blkRpt + 1
            Else
                RichTextBox1.AppendText(sring & vbNewLine)
           End If

           Dim s1 As String = TextBox1.Text
           Dim len1 As Integer = TextBox2.Text
           Dim r1 As New Random
           Dim sb1 As New StringBuilder

           For i As Integer = 1 To len1
               Dim idx1 As Integer = r1.Next(0, s1.Length)
               sb1.Append(s1.Substring(idx1, 1))
           Next

           Dim sring1 As String = sb1.ToString

           If RichTextBox1.Text.Contains(sb1.ToString) Then
                blkRpt = blkRpt + 1
           Else
                RichTextBox1.AppendText(sring1 & vbNewLine)
           End If

           Dim s2 As String = TextBox1.Text
           Dim len2 As Integer = TextBox2.Text
           Dim r2 As New Random
           Dim sb2 As New StringBuilder

           For i As Integer = 1 To len2
               Dim idx2 As Integer = r2.Next(0, s2.Length)
               sb2.Append(s2.Substring(idx2, 1))
           Next

           Dim sring2 As String = sb2.ToString

           If RichTextBox1.Text.Contains(sb2.ToString) Then
               blkRpt = blkRpt + 1
           Else
               RichTextBox1.AppendText(sring2 & vbNewLine)
           End If

           Dim s21 As String = TextBox1.Text
           Dim len21 As Integer = TextBox2.Text
           Dim r21 As New Random
           Dim sb21 As New StringBuilder

           For i As Integer = 1 To len1
               Dim idx21 As Integer = r21.Next(0, s2.Length)
               sb2.Append(s2.Substring(idx21, 1))
           Next

           Dim sring21 As String = sb21.ToString

           If RichTextBox1.Text.Contains(sb21.ToString) Then
               blkRpt = blkRpt + 1
           Else
               RichTextBox1.AppendText(sring21 & vbNewLine)
           End If

           If RichTextBox1.Lines.Length - 1 >= perm Then
               Exit For
           End If

           Label6.Text = blkRpt.ToString
           Label5.Text = RichTextBox1.Lines.Length - 1 & "/" & perm
       Next
    End Sub
End Class
share|improve this question
Please use Option Strict....implicit conversions (like Dim rtb As Double = TextBox2.Text) are dangerous. What is TextBox2.Text has "The first and the last" in it? – Tim Apr 14 at 8:56
Have you run this code? Your outer for loop has Step 0 - which means the loop will never increment - you have an infinite loop. You're using random to get (I think) a random element of the string...but you could easily get the same element multiple times. Those are just two things that jump out. – Tim Apr 14 at 9:05
1  
Take a look at this SO question - Listing all permutations of a string/integer. Goes into a good deal of detail on how to do this. – Tim Apr 14 at 9:06
I have ran this code, It will continue to loop until it generated all the possible combination – XtraCode Apr 14 at 10:54
Every output in Richtextbox1 is unique. Look at richtextbox1.text.contains(sb.ToString) – XtraCode Apr 14 at 10:55
show 1 more comment

migrated from stackoverflow.com Apr 17 at 12:13

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.