Solved

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

Posted on 2013-05-21
3
310 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now