Solved

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

Posted on 2013-05-21
3
316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

751 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