Results 1 to 2 of 2

Thread: Random line from a file?

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 1999
    Posts
    12
    Can someone give me the code to choose a random line from a specified file?

    Thank you

  2. #2
    Hyperactive Member Juan Carlos Rey's Avatar
    Join Date
    Aug 1999
    Location
    Mendoza, Argentina
    Posts
    301

    Random

    NOT TESTED:!

    Code:
    Dim a as String, I as Integer, K as Integer, N as Integer
    Open File For Input as #1
    While Not EOF (1)
    Line Input #1, a ' use Line Input method to avoid commas, etc.
    I = I + 1 ' counts the available lines
    Next
    Close
    Randomize
    K = Rnd * I ' selects a number in the order of the available lines
    Open File For Input as #1
    For N = 1 to K
    Line Input #1, a ' displaces pointer to randomly selected line
    Next
    Line Input #1, a ' inputs that line
    Close
    ' Now Do Your Stuff With Your Line!
    HTH!

    Combat poverty: kill a poor!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width