Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-05-21
3
Medium Priority
?
320 Views
Last Modified: 2013-05-21
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:

\\ServerA\input\sobi\05172013040604\16624-20130206-FTO-1.pdf|16624|_||TestDG|TestDT|TestDST|250|05/07/2013|
\\ServerA\input\sobi\05172013040604\16624-20130206-FTO-2.pdf|16624|_||TestDG|TestDT|TestDST|250|05/07/2013|
\\ServerA\input\sobi\05172013040604\16624-20130206-FTO-3.pdf|16624|_||TestDG|TestDT|TestDST|250|05/07/2013|

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:

\\ServerA\input\sobi\05172013040604\16624-20130206-FTO-1.pdf|16624|_||TestDG|TestDT|TestDST|340|05/07/2013|
\\ServerA\input\sobi\05172013040604\16624-20130206-FTO-2.pdf|16624|_||TestDG|TestDT|TestDST|854|05/07/2013|
\\ServerA\input\sobi\05172013040604\16624-20130206-FTO-3.pdf|16624|_||TestDG|TestDT|TestDST|188|05/07/2013|

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)

        BackgroundWorker1.ReportProgress(50)

        '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
        Next

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

        BackgroundWorker1.ReportProgress(65)

Open in new window


Kindly advise,

Regards,
N
0
Comment
Question by:nobushi
3 Comments
 
LVL 11

Expert Comment

by:apathy42
ID: 39184953
Check this thread out, I believe the comment on this matches what you're looking for:

http://stackoverflow.com/questions/18676/random-int-in-vb-net
0
 
LVL 14

Accepted Solution

by:
quizwedge earned 2000 total points
ID: 39184969
Move randomnumber = randomclass.Next(100, 999) to inside the for each loop

   'Define random number for comment field of each file

        Dim randomclass As New System.Random()
        Dim randomnumber As Integer

        BackgroundWorker1.ReportProgress(50)

        '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)
            randomnumber = randomclass.Next(100, 999)
            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
        Next

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

        BackgroundWorker1.ReportProgress(65)

Open in new window

0
 
LVL 1

Author Closing Comment

by:nobushi
ID: 39185076
Thank you.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Screencast - Getting to Know the Pipeline

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question