Hey people!

I have this code:

Public Class Form1
    Private HowManytoGenerate As Integer
    Private HowManyDone As Integer
    Private List As ArrayList
    Private CDKeyFile As String = "C:\My Cool CD Keys.txt"

    Public Function GenerateUniqueString() As String

        Dim rnd As New Random
        Dim Chars As String
        Dim Result As String = ""
        Dim Part1 As String = ""
        Dim Part2 As String = ""
        Dim Part3 As String = ""
        Dim Part4 As String = ""
        Dim Part5 As String = ""

        Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

        While Part1.Length <= 5
            Part1 &= Chars.Chars(rnd.Next(0, Chars.Length))
        End While

        While Part2.Length <= 5
            Part2 &= Chars.Chars(rnd.Next(0, Chars.Length))
        End While

        While Part3.Length <= 5
            Part3 &= Chars.Chars(rnd.Next(0, Chars.Length))
        End While

        While Part4.Length <= 5
            Part4 &= Chars.Chars(rnd.Next(0, Chars.Length))
        End While

        While Part5.Length <= 5
            Part5 &= Chars.Chars(rnd.Next(0, Chars.Length))
        End While

        Result = Part1 & "-" & Part2 & "-" & Part3 & "-" & Part4 & "-" & Part5

        Return Result


    End Function

    Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
        TextBox1.Text = GenerateUniqueString()
    End Sub

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


    Private Sub Generate_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles Generate.DoWork
        IO.File.WriteAllText(CDKeyFile, "")

        HowManytoGenerate = NumericUpDown1.Value
        HowManyDone = 0

        ProgressBar1.Maximum = NumericUpDown1.Value
        ProgressBar1.Value = 0

        Do Until HowManyDone = HowManytoGenerate
            IO.File.AppendAllText(CDKeyFile, GenerateUniqueString() & vbNewLine)
            HowManyDone += 1
            ProgressBar1.Value = HowManyDone
        Loop
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False
    End Sub
End Class

The batch section should generate a new random value and write it every time it do the loop, but...

It is repeating values. There are more than one line with the same value. What I'm doing wrong?

Line 9: You declare the random variable inside your method. Since it uses a seed off the clock, and your computer is very fast, it generates the same random sequence with each call. Move it outside the method to be a class variable and all should be good.

This question has already been answered. Start a new discussion instead.