vb.net - get random name from text file

hello there,
I have this little function that I am using to get a random name from a text file but sometimes when the text file is over 350KB in size it just takes long to get a random name..
how can I make things speed up more?

Public Function GetRandomName(ByRef TexFile As String) As Object
        Dim strFile, strInt As Integer, strName() As String = Nothing, strLine As String
        strFile = FreeFile()
        FileOpen(strFile, TexFile, OpenMode.Input)
        Do Until EOF(strFile)
            strLine = LineInput(strFile)
            ReDim Preserve strName(strInt)
            strName(strInt) = strLine
            strInt = strInt + 1
        Loop
        FileClose(strFile)

        GetRandomText = strName(Int(Rnd() * (UBound(strName) + 1)))
    End Function

Open in new window

LVL 1
XK8ERAsked:
Who is Participating?
 
SoLostConnect With a Mentor Commented:
Or, since you're reading the entire file already...
Private Function GetRandomName2(ByVal Filename As String) As String
        Dim Result As String = ""
        Dim SR As IO.StreamReader = Nothing
        Dim Names() As String = {}

        SR = New IO.StreamReader(Filename)

        ' Read the entire file and seperate the lines into an array
        Names = Split(SR.ReadToEnd, vbCrLf)

        With SR
            .Close()
            .Dispose()
        End With
        SR = Nothing

        ' Retrieve a random name from the list
        Result = Names(Int(Names.Count * Rnd() + 1) - 1)

        Return Result
    End Function

Open in new window

0
 
SoLostCommented:
I'm not sure if this will help but you could try doing it using the StreamReader and List classes.

They might have better performance than continually expanding the size of your array.


Otherwise, if you know how many lines there are in the file you could just read up to that line wihtout having to parse the entire file every time.
Private Function GetRandomName(ByVal Filename As String) As String
        Dim Result As String = ""
        Dim SR As IO.StreamReader = Nothing
        Dim Names As New List(Of String)

        SR = New IO.StreamReader(Filename)
        While SR.Peek >= 0
            ' Add each name to the list
            Names.Add(SR.ReadLine)
        End While

        With SR
            .Close()
            .Dispose()
        End With
        SR = Nothing

        ' Retrieve a random name from the list
        Result = Names(Int(Names.Count * Rnd() + 1) - 1)

        Return Result
    End Function

Open in new window

0
 
XK8ERAuthor Commented:
the file always remains the same so maybe not read the entire file every time?
im clicking on the button and its taking 6 seconds to finish every time!!
0
 
XK8ERAuthor Commented:
ohh just saw your code.. thanks it works perfectly fine!!
0
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.

All Courses

From novice to tech pro — start learning today.