Solved

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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