vb.net randomizer

Posted on 2013-09-05
Medium Priority
Last Modified: 2013-09-05
how can I make a little tool that can randomize a text file I have..
for example I have some ids like this


Im looking for a way to have it change and be all random.. how can I do this?
Question by:XK8ER
1 Comment
LVL 64

Accepted Solution

Fernando Soto earned 2000 total points
ID: 39469183

The following subroutine will do what you want to do.

Private Sub RandomizeLines(inputPath As String, outputPath As String)

    ' Read all lines from file with ID's into memory, List object
    Dim lines As List(Of String) = File.ReadAllLines(inputPath).ToList()
    ' Get the number of line read
    Dim count As Integer = lines.Count()
    ' Array to hold randomized lines
    Dim linesRand() As String = New String(count - 1) {}
    ' Get a random number generator
    Dim rand As Random = New Random(DateTime.Now.Millisecond)

    ' For each line of the input file
    For Each line In lines
        ' Get a random number between 0 and max lines in file
        Dim idx As Integer = rand.Next(0, count - 1)
        ' Check to see if array slot is empty if not get a nuw random number
        While linesRand(idx) IsNot Nothing
            idx = rand.Next(0, count)
        End While
        ' Fill the empty slot with a line
        linesRand(idx) = line

    ' Write the array out to the file system
    File.WriteAllLines(outputPath, linesRand)

End Sub

Open in new window


Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…
Watch the video to know the simple way to remove or recover or reset lost or forgotten passwords of Outlook PST file. With Kernel Outlook Password Recovery tool such operation is very easy to perform. It is a freeware with limitation to use with 500…
Suggested Courses

622 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