Display random tips from text file

Posted on 2007-08-02
Last Modified: 2013-12-26

  I have a text file full of one line tips that I'd like to randomly display on screen. Does someone have some nice code that will display random lines from that file?

Question by:tvtech
    LVL 53

    Expert Comment

    Is it for vb6 or ?

    There are numerous ways in which this could be achieved - it rather depends upon how you are storing the data once you have read it from the file.
    The following bit of code may give you some ideas.  It simply reads each line from a text file, adding a four digit random number string to the front of each line, before adding the line to a list.  The list is then sorted, which randomises the original order, and the four digit number is removed.
    Code Snippet
    Dim InputData As New List(Of String)
    Dim Rnd As New Random
    Dim SR As New IO.StreamReader("d:\temp\test.txt")
    Do While Not SR.EndOfStream
       InputData.Add(Rnd.Next(0, 10000).ToString("0000") & SR.ReadLine)
    For count As Integer = 0 To InputData.Count - 1
       InputData(count) = InputData(count).Substring(4)

    LVL 53

    Accepted Solution

    Example for vb6

    Dim A() As String, I As Long
        I = -1
        Open "Your txt path" For Input As #1
        While Not EOF(1)
            I = I + 1
            ReDim Preserve A(I)
            Line Input #1, A(I)
        Randomize (Timer)
        MsgBox A(Int((UBound(A) * Rnd) + 1))

    Author Comment

    Djaest, just perfect! Thanks for a quick and accurate reply.


    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now