Solved

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

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

16 Experts available now in Live!

Get 1:1 Help Now