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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
SoLostCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.