Link to home
Create AccountLog in
Avatar of Kevin
KevinFlag for United States of America

asked on

VB.Net - Issue with random number being written to text file

Good Morning,

I am using Visual Studio Express 2012 for a windows form with VB.Net.

My code below basically works for its intended purpose, which is, depending on what the user selects on the form will be written in a text file.

Final result of the text file looks like this:


Open in new window

My issue is that my random number when generated is not as random as I would like it. As you can see from the example above the random number is “250” for all of the lines of the output. When each line should have a different random number example below:


Open in new window

I’m thinking the reason the random number is all the same for each line is because my code is creating the output all at one go and this is why each line is using the same random number.

I’m fairly new to VB.Net, would someone be able to tell me how or what code I am to use to ensure each line written to the text file has a different random number?

Part of my code is below.

        'Define random number for comment field of each file

        Dim randomclass As New System.Random()
        Dim randomnumber As Integer
        randomnumber = randomclass.Next(100, 999)


        'create txt file from dest directory of all files to be indexed.

        Dim str As String
        For Each rfiles As String In System.IO.Directory.GetFiles(destdir)
            str = str & rfiles & "|" & System.IO.Path.GetFileNameWithoutExtension(rfiles).Split("-")(0).Trim & "|" & "_" & "||" & DataGridView1.SelectedRows(0).Cells(0).Value & "|" & DataGridView1.SelectedRows(0).Cells(1).Value & "|" & DataGridView1.SelectedRows(0).Cells(2).Value & "|" & randomnumber & "|" & DateTimePicker1.Text & "|" & Environment.NewLine

        Dim outputname As String = [String].Format("\\ServerA\input\sobi{0}.txt", DateTime.Now.ToString("MMddyyyyhhmmss"))
        System.IO.File.WriteAllText(outputname, str)


Open in new window

Kindly advise,

Avatar of apathy42
Flag of United States of America image

Check this thread out, I believe the comment on this matches what you're looking for:
Avatar of quizwedge
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Kevin


Thank you.