Random Order Algorithm

Cheetah

Well-known member
Joined
Oct 12, 2006
Messages
232
Programming Experience
Beginner
Hi there,

I need an algorithm or the jist of how it would work for the following.

I have a .txt file with x amount of lines in it. I need to randomly pick each line and add it to a list, but all the lines must be added.

Thanks.

EDIT:

I dont hav ethe problem with the reading of the text file. Its the random order of reading bit.
 
VB.NET:
Dim lines As String() = IO.File.ReadAllLines("file path here")
Dim indexes As New List(Of Integer)

For i As Integer = 0 To lines.GetUpperBound(0) Step 1
    indexes.Add(i)
Next i

Dim jumbledLines As New List(Of String)
Dim indexGenerator As New Random
Dim index As Integer

While indexes.Count > 0
    index = indexGenerator.Next(0, indexes.Count)
    jumbledLines.Add(lines(indexes(index)))
    indexes.RemoveAt(index)
End While

MessageBox.Show(String.Join(Environment.NewLine, jumbledLines.ToArray()))
 
Back
Top